在http://guides.rubyonrails.org/layouts_and_rendering.html上名为 'Layouts and Rendering in Rails' 的 Ruby On Rails 指南中使用 :alert (或 :notice)和 render 方法对我不起作用
这是指南中提供的示例代码:
def index
@books = Book.all
end
def show
@book = Book.find_by_id(params[:id])
if @book.nil?
@books = Book.all
render "index", :alert => 'Your book was not found!'
end
end
我有一个看起来像这样的 hello 控制器:
class HelloController < ApplicationController
def index
@counter = 5
end
def bye
@counter = 4
render "index", :alert => 'Alert message!'
end
end
我的 index.html.erb 视图如下所示:
<ul>
<% @counter.times do |i| %>
<li><%= i %></li>
<% end %>
</ul>
访问时http://localhost:3000/hello/bye
,我看到索引视图,即按预期显示从 1 到 4 的数字列表,但没有“警报消息!” 警报显示。
我的布局使用它来显示警报消息:
<% flash.each do |k, v| %>
<div id="<%= k %>"><%= v %></div>
<% end %>