I don’t know if this is a good idea, but I have done something similar in one project, and found it to work well in my case. I only have one nested resource though. For this reason, I have defined the plug as a function in controller for the nested resource, e.g.:
defmodule MyApp.CommentController do
...
plug(:assign_blog)
...
case Blogs.get_blog(conn.params["blog_id"]) do
%Blog{} = blog ->
assign(conn, :blog, blog)
nil ->
raise Error.exception("Blog not found", 404)
end
...
end
If you have several controllers which need the blog resource, then I’d suggest putting that function in a dedicated module (like a “real” plug).


















