ksherman

ksherman

Accessing parameters in nested route resources

Hiya! Super new to the Elixir/Phoenix lang (enjoying it so far!). This might be a really silly question, but have had a hard time finding resources/examples.

So I have a nested route resources, e.g.

resources "/users", UserController, except: [:new, :edit] do
  resources "/lists", ListController, except: [:new, :edit]
end

And then in list_controller, just a basic index:

def index(conn, _params) do
  user_lists = Users.list_user_lists()
  render(conn, "index.json", user_lists: user_lists)
end

So I’d like to scope this index to the lists that belong to the user_id in this route. There’s obviously a lot of examples with pulling in the immediate related record, e.g. def show(conn, %{"id" => id}) do which pulls in the current route’s list_id, but how do I also get the user record?

My one work-around so far in the ListController.create( ) action is to pull it out of params, something like

Users.create_list(list_params, Users.get_user!(conn.params["user_id"]))

Is there a better way I’m missing? Thanks!

Most Liked

BrightEyesDavid

BrightEyesDavid

Hi! Excuse the brevity; I’m on my phone. This is also untested and based on a quick read of the controller guide (Controllers — Phoenix v1.7.10) and my possibly incorrect memory. (It’s been a while since I’ve worked on my Elixir/Phoenix project!)

That being said, I think the usual way for your lists controller would be something like:

def index(conn, %{"user_id" => user_id}) do
  user_lists = Users.list_user_lists(user_id)
  render(conn, "index.json", user_lists: user_lists)
end

def show(conn, %{"user_id" => user_id, "id" => id}) do
  list = Users.get_user_list(id)
  render(conn, "show.json", list: list)
end

From the guide:

It is a good practice to pattern match against params in the function signature to provide data in a simple package we can pass on to rendering.

In the show action above, it may not be necessary to include user_id (and its binding to ‘user_id’) in the pattern match, depending on whether or not the user id is needed when getting the list/preparing data for the view.

Related: see the output of mix phx.routes as per Routing — Phoenix v1.7.10 to see the param names of your router’s routes to use in the pattern matching.

Where Next?

Popular in Questions Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
dblack
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
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
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New

Other popular topics Top

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43591 214
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
dblack
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement