How to debug eex template and @ variables?

故事扮演 提交于 2019-12-23 10:06:59

问题


I have this template

  <%= form_for @changeset, bid_path(@conn, :update, 1), [method: :put], fn f -> %>
    <% 
      require IEx
      IEx.pry
    %>
<%= if @changeset.action do %>
  <div class="alert alert-danger">
    <p>Oops, something went wrong! Please check the errors below.</p>
  </div>
<% end %>

How can I display @changeset in IEx console? When I'm trying to do this is blows an error:

pry(5)> @changeset
** (ArgumentError) cannot invoke @/1 outside module
    (elixir) lib/kernel.ex:3960: Kernel.assert_module_scope/3
    (elixir) expanding macro: Kernel.@/1
             web/templates/bid/edit.html.eex:5: (file)

回答1:


@ in eex templates in Phoenix is completely unrelated to @ in Elixir/iex. In eex templates in Phoenix, @foo is roughly equivalent to Access.fetch!(assigns, :foo), while in Elixir/iex, they're used to define module attributes. So, in order to access @changeset in iex, you can can do:

Access.fetch!(assigns, :changeset)

or just the following if you're okay with getting nil for non-existent keys:

assigns[:changeset]


来源:https://stackoverflow.com/questions/39386255/how-to-debug-eex-template-and-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!