How can I interpolate an assign within a link helper?

I have the following routes and controller action.

 scope "/", StarTrackerWeb do
    pipe_through(:browser)

    get("/", PageController, :index)
    get("/info/", PageController, :about)
    get("/info/:name", PageController, :about)
    get("/info/:name/:position", PageController, :about)
  end
def about(conn, params) do
    names = ["Jose", "Chris", "Jeffrey"]
   
      render(
      conn,
      "information.html",
      name: params["name"],
      position: params["position"],
      names: names
       )
  end

The goal is to interpolate the @position assign within this link helper in my template.

    <%= for name <- @names do %>
        <li><%= link "#{name} the ", to: Routes.page_path(@conn, :about) %></li>
    <% end %>
</ul>

So, given the URL “/info/jose/engineer”.

The generated link text should be:

jose the engineer

However, neither calling the assigns directly or interpolating them works.

<%= link "#{name} the @position ", to: Routes.page_path(@conn, :about) %></li>

<%= link "#{name} the #{position} ", to: Routes.page_path(@conn, :about) %></li>

You’re almost there:

<%= link "#{name} the #{@position} ", to: Routes.page_path(@conn, :about) %></li>
2 Likes