dli
Thoughts on resolving nested resources using Plug
Just saw this blog post by @leejarvis: https://leejarvis.me/posts/2020/phoenix-simplify-nested-resources
The post explains how to resolve nested resources in a central place.
post_comment_path GET /projects/:project_id/posts/:post_id/comments AppWeb.CommentController :index
post_comment_path GET /projects/:project_id/posts/:post_id/comments/:id/edit AppWeb.CommentController :edit
post_comment_path GET /projects/:project_id/posts/:post_id/comments/new AppWeb.CommentController :new
[…] every one of our controller actions are going to need to handle this
project_idparameter.I come from a Rails background, and the canonical way to solve this in Rails is to add a
before_action:
class CommentController
before_action :set_project
# actions
def set_project
@project = Project.find(params[:project_id])
end
end
The author then uses a custom Plug to add the project to conn.assigns:
defmodule AppWeb.CommentController do
use AppWeb, :controller
plug :put_project
def show(conn, %{"post_id" => post_id, "id" => comment_id}) do
%{current_project: project} = conn.assigns
render(conn, "show.html", comment: get_comment!(project, post_id, comment_id))
end
defp put_project(conn, _opts) do
current_project = fetch_current_project(conn.params["project_id"])
assign(conn, :current_project, current_project)
end
end
I am curious about your opinions. How do you handle this in your projects?
Popular in Discussions
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
Hello everyone,
I’m so glad to have discovered this awesome community. Thanks for creating it! This is my second post, and apologies for...
New
How We Replaced React with Phoenix
By: Thought Bot
New
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
I’m still quite new to Elixir.
As I understand we got in Elixir “multi guards” as convention to simplify one large guard with or’s?:
de...
New
Please, let me know if this kind of discussion already took place in another topic
.
Hi all, how do you consider if is better to build ...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Hey everyone, this has been on my mind for some time and I’d love your input on it!
TLDR: I feel like maps are superioer for storing and...
New
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
Hey guys!
I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
Other popular topics
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








