在Rails中呈现操作时不会显示警报

at.*_*at. 2 alert controller ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

我编写了以下代码来显示show.html.erb带有警报flash消息的视图:

render action: 'show', alert: 'Please fix mistakes outlined in red'
Run Code Online (Sandbox Code Playgroud)

但是,警报不会出现.但是会出现通知.我认为render action: 'show'上面的调用是错误的,因为我只是通过直觉写道.这是我的html.erb代码:

<%if notice%><p id="notice"><%= notice %></p><%end%>
<%if alert%><p id="alert"><%= alert %></p><%end%>
Run Code Online (Sandbox Code Playgroud)

cde*_*ers 6

处理闪存的更标准方法是在视图布局中编写以下内容

<% flash.each do |key, value| %>
    <p class="alert alert-<%= key %>"><%= value %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud)

并在您的控制器中写下以下内容

flash[:alert] = 'Please fix mistakes outlined in red'
render 'show'
Run Code Online (Sandbox Code Playgroud)