Home > Enterprise >  How can I interpolate an assign within a link helper?
How can I interpolate an assign within a link helper?

Time:01-04

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>

CodePudding user response:

Add the @ sign to position to get it to render in the page:

Change this:

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

To this:

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