Parameters in Routes_helper_path

Hello, I’m a bit confused how params work in the Routes_helper_path. Example:

In the User template, show.html.eex, i can do this:

<span><%= link "Editar", to: Routes.user_path(@conn, :edit, @user), class: "btn btn-primary" %></span>

but if i wanted to pass it to another controller like this:

<span><%= link "Agregar Cronico", to: Routes.pedido_path(@conn, :new, @user), class: "btn btn-primary" %></span>

I get error: protocol Enumerable not implemented for %App.Users.Users{..

Also, there is a correct way to pass parameters to another controller?. Thanks

I think you should pass the user id as a keyword list element like

user_id: @user.id

And then you could pattern-match it in pedido controller’s new function’s parameters map as

%{“user_id” => user_id}

Hth.

2 Likes

Thanks, i would use that. Still intrigued why cant just pass @user, like the other example.

A new action does not expect any id params, but edit does… so the 3rd argument should be an enumerable, not a struct.

You might see this in your routes. You will see edit needs an :id, but not new. Unless it is a nested controller. But your path shows it is not nested.

mix phx.routes
2 Likes