@conn argument to Phoenix link HTML Helper

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!

Page 55
https://media.pragprog.com/titles/phoenix14/code/controllers_views_templates/listings/rumbl/lib/rumbl_web/templates/user/index.html.eex

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.

https://hexdocs.pm/phoenix/templates.html#examples

https://hexdocs.pm/plug/Plug.Conn.html#assign/3

1 Like

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/….

2 Likes