Elixir Phoenix 闪现消息不显示

Elixir Phoenix flash messages do not show up

我正在尝试在 Phoenix 应用程序中显示即显消息,但它们似乎没有显示。我尝试了很多不同的东西,其中之一是:

<div class="row">
  <div class="col-sm-12">
    <%
    info = get_flash(@conn, :info)
    error = get_flash(@conn, :error)
    %>
    <% if info do %>
      <div class="alert alert-info" role="alert"><%= info %></div>
    <% end %>
    <% if error do %>
      <div class="alert alert-danger" role="alert"><%= error %></div>
    <% end %>
  </div>
</div>

谁能告诉我正确的代码?

您需要使用 <%= 而不是 <%

<%= if info do %>
  <div class="alert alert-info" role="alert"><%= info %></div>
<% end %>

来自the docs

All expressions that output something to the template must use the equals sign (=). Since everything in Elixir is an expression, there are no exceptions for this rule. For example, while some template languages would special-case if clauses, they are treated the same in EEx and also require = in order to have their result printed: