rails 中未显示即显消息

Flash message not showing in rails

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

这是我的部分表单视图

<%= 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>

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

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

Flash 提供了一种在操作之间传递临时基元类型(字符串、数组、哈希)的方法。但是您正在尝试将 flash 通知作为 url_redirection 参数发送。 只需声明

flash[:notice] = "some msg"

在您重定向之前。

http://api.rubyonrails.org/classes/ActionDispatch/Flash.html

尝试使用以下代码显示 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

app/views/layouts/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>