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
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
Hey everone!
I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
Another point that Dave drives home in his course is that applications really are components, and you should treat them as such. Not as “...
New
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
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
Other popular topics
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Latest Phoenix Threads
Chat & Discussions>Discussions
Latest on Elixir Forum
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
- #code-sync
- #javascript
- #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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









