Flash 消息未显示在 Rails 中

Muh*_*lah 2 ruby-on-rails flash-message

我在尝试使用 Flash 消息时遇到了一些问题flash[:notice]。flash 消息没有显示它的消息

这是我的部分表单视图

<%= form_tag bulk_push_api_v1_notifications_path do |f| %>
<fieldset class="inputs">
    <legend>
        <span>Details</span>
    </legend>
        <% if flash[:notice].present? %>
          <p class='flash-notice'><%= flash[:notice] %></p>
        <% elsif flash[:error].present? %>
          <p class='flash-error'><%= flash[:error] %></p>
        <% end %>
    <ol>
        <li class="file input required" id="play_media_input">
             <%= label_tag(:message, "Message : ") %>
             <%= text_area_tag :message,  nil, :required => true %>
            <p class="inline-hints">Only text can be sent</p>
        </li>
    </ol>
</fieldset>
<fieldset class="actions">
    <ol>
        <li class="action input_action " id="play_submit_action">
            <%= submit_tag("Send Notification") %>
        </li>
    </ol>
</fieldset>
Run Code Online (Sandbox Code Playgroud)

它会从控制器触发这个方法

def bulk_push
  begin
    User.send_bulk_notifications(params[:message])
    redirect_to admin_notification_path, :flash => { :notice => "Insufficient rights!" }
  rescue
    redirect_to admin_notification_path, :flash => { :error => "Error" }
  end

end
Run Code Online (Sandbox Code Playgroud)

pun*_*t18 5

尝试下面的代码来显示flash消息:

控制器

def bulk_push
  begin
    User.send_bulk_notifications(params[:message])
    redirect_to admin_notification_path, notice: "Insufficient rights!"
  rescue
    redirect_to admin_notification_path, alert: "Error"
  end

end
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/布局/application.html.erb

<% if notice %>
  <p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
  <p class="alert alert-danger"><%= alert %></p>
<% end %>

<style type="text/css">
  .alert-success{
    color: green;
  }
  .alert-danger{
    color: red;
  }
</style>
Run Code Online (Sandbox Code Playgroud)