http://guides.rubyonrails.org/layouts_and_rendering.htmlの 'Layouts and Rendering in Rails' と呼ばれる Ruby On Rails ガイドの render メソッドで :alert (または :notice) を使用してもうまくいきません。
これが、ガイドで提供されているサンプル コードです。
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 %>