【问题标题】:Flash message not showing in rails闪烁消息未显示在导轨中
【发布时间】:2017-11-28 13:42:52
【问题描述】:

我在尝试使用 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>

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

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

【问题讨论】:

    标签: ruby-on-rails flash-message


    【解决方案1】:

    试试下面的代码来显示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>
    

    【讨论】:

    • 非常感谢先生,对我很有帮助
    • @MuhammadRahmatullah 如果它适合您,请投票给我的答案。谢谢
    • 当然,先生,但目前我在支持您的回答时遇到了一些错误Daily vote limit reached; vote again in 16 hours.
    • @MuhammadRahmatullah Ok Np
    【解决方案2】:

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

    flash[:notice] = "some msg"
    

    在重定向之前。

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

    【讨论】:

    • 它仍然不起作用,先生,我已按照您的建议更改了代码
    猜你喜欢
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 2014-10-23
    • 2018-06-14
    • 1970-01-01
    • 2018-09-24
    相关资源
    最近更新 更多