Pet*_*lis 6 ruby ruby-on-rails partials ruby-on-rails-3 railstutorial.org
可能重复:部分中的
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>
Run Code Online (Sandbox Code Playgroud)
这很好用.
但是,我尝试通过在我的部分文件夹中创建_flash.html.erb来清理此代码...
<% flash.each do |key,value| %>
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
<!-- <div class="alert alert-<%= key %>"><%= value %></div> -->
<% end %>
Run Code Online (Sandbox Code Playgroud)
......而不是......
<%= render 'partials/flash' %>
Run Code Online (Sandbox Code Playgroud)
...在我的应用程序布局中,我的所有Rspec测试都开始失败,每个测试都有以下消息:
Failure/Error: before { visit signup_path }
ActionView::Template::Error:
undefined method `each' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
关键问题似乎是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 %>
Run Code Online (Sandbox Code Playgroud)
...产生与上述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 %>
Run Code Online (Sandbox Code Playgroud)
...打破flash消息的工作(因为'如果flash'总是假的).
我有两个相关的问题:
为什么/如何使用partials/flash解决方案改变rails应用程序的行为?
如何更改我的部分/闪存以便它可以工作?
谢谢!
使用参数/值的哈希值设置局部变量以传递给部分
<%= render :partial => "partials/flash", :locals => {:flash => flash} %>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3564 次 |
最近记录: |