【问题标题】:How can I interpolate an assign within a link helper?如何在链接助手中插入分配?
【发布时间】:2022-01-03 21:34:05
【问题描述】:

我有以下路线和控制器动作。

 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

目标是在我的模板中的此链接帮助程序中插入 @position 分配。

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

所以,给定 URL“/info/jose/engineer”。

生成的链接文本应该是:

何塞工程师

但是,直接调用分配或插入它们都不起作用。

&lt;%= link "#{name} the @position ", to: Routes.page_path(@conn, :about) %&gt;&lt;/li&gt;

&lt;%= link "#{name} the #{position} ", to: Routes.page_path(@conn, :about) %&gt;&lt;/li&gt;

【问题讨论】:

    标签: html elixir phoenix-framework


    【解决方案1】:

    @ 符号添加到position 以使其在页面中呈现:

    改变这个:

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

    到这里:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 2013-02-25
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      相关资源
      最近更新 更多