可能重复:
部分中的 Flash 消息(Rails 3)
我正在做 Michael Hartl 的 Railstutorial 并列出 7.26将 flash 消息添加到应用程序布局:
<!DOCTYPE html>
<html>
.
.
.
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
.
.
.
</body>
</html>
这工作正常。
但是,我试图通过在我的部分文件夹中创建一个 _flash.html.erb 来清理这段代码......
<% flash.each do |key,value| %>
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
<!-- <div class="alert alert-<%= key %>"><%= value %></div> -->
<% end %>
...而不是使用...
<%= render 'partials/flash' %>
...在我的应用程序布局中,我的所有 Rspec 测试都开始失败,每个测试都显示以下消息:
Failure/Error: before { visit signup_path }
ActionView::Template::Error:
undefined method `each' for nil:NilClass
关键问题似乎是 flash 为零,因为将我的 _flash 部分包装在这样的 if 语句中......
<% unless flash.empty? %>
<% flash.each do |key,value| %>
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
<!-- <div class="alert alert-<%= key %>"><%= value %></div> -->
<% end %>
<% end %>
...产生与上述有关 NilClass 的相同错误消息,并将其包装在这样的 if 语句中...
<% if flash %>
<% flash.each do |key,value| %>
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
<!-- <div class="alert alert-<%= key %>"><%= value %></div> -->
<% end %>
<% end %>
... 使 flash 消息无法正常工作(因为 'if flash' 始终为假)。
我有两个相关的问题:
为什么/如何使用 partials/flash 解决方案改变 Rails 应用程序的行为?
如何更改我的部分/闪存,以便它可以工作?
谢谢!