凤凰通过变量查看 [英] phoenix pass variable to view

查看:85
本文介绍了凤凰通过变量查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将在视图中使用的查询结果,但是当我在视图中调用变量时,却得到了undefined function data/0(因为数据不起作用)

i have a query result that will be used in view, but when i call the variable in view, i got undefined function data/0 (because data is not function)

我已经用不同的方式,地图等传递数据了

i already pass data with different way, map, etc

controller.ex:

controller.ex :

def new(conn, _params) do
    data =
        Enum.map(
          from(AccountTypeSchema)
          |> Repo.all(),
          fn elem ->
            elem |> Map.from_struct() |> Map.delete(:__meta__)
          end
        )
    render(conn, "new.html", data: data)
end

view.html.eex:

view.html.eex :

<% Enum.each data, fn(item) -> %>
    <option value="us">Facebook</option>
<% end %>

从视图中调用data变量

推荐答案

请改用此代码:

render(conn, "new.html", account_types: data)

然后查看:

<% Enum.each @account_types, fn(item) -> %>
  <option value="us">Facebook</option>
<% end %>

通常,在传递关键字列表或映射到视图时,模板中会提供@前缀变量: https://hexdocs.pm/phoenix/Phoenix.View.html#render/3

Generally you have @ prefixed variables available in template when you pass keyword list or map to a view: https://hexdocs.pm/phoenix/Phoenix.View.html#render/3

这篇关于凤凰通过变量查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆