in one index.html.eex example in the book Programming Phoenix you have this example
link “View”, to: Routes.user_path(@conn, :show, user.id)
The question is why is @conn is passed to Routes.user_path and why as @conn not as conn?
Thanks.
in one index.html.eex example in the book Programming Phoenix you have this example
link “View”, to: Routes.user_path(@conn, :show, user.id)
The question is why is @conn is passed to Routes.user_path and why as @conn not as conn?
Thanks.
Welcome to the forum!
Routes.user_path/3
generates the user
controller URL based on the routing entries and the contents of the conn
structure.
https://hexdocs.pm/phoenix/routing.html#resources
https://hexdocs.pm/phoenix/routing.html#path-helpers
@conn
in references the conn
value in the plug’s assigns
map.
Nitpick: …in the view’s assigns map.
Views are not really part of the plug pipeline. The conn’s assigns are usually merged into the assigns passed to the view though.
As for the reason why you need the conn for generating a path: The phoenix app can be mounted at a subfolder of a domain a.k.a. http://example.com/phoenix
. Without the conn (or other possible first argument for path helpers) the result would be missing the leading /phoenix/…
.