dli

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_id parameter.

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?

Where Next?

Popular in Discussions Top

vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
Donovan
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
praveenperera
How We Replaced React with Phoenix By: Thought Bot
New
crispinb
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
crabonature
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
marciol
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
boundedvariable
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
PragTob
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
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
tomekowal
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 Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
joeerl
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
sergio_101
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
bsollish-terakeet
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
boundedvariable
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement