【问题标题】:How to modify bootstrap flash message in rails 4如何在 Rails 4 中修改引导闪存消息
【发布时间】:2014-09-26 16:46:55
【问题描述】:

我在引导闪存消息中遇到问题。我必须更改 Flash 消息中某些文本的颜色。

控制器

redirect_to complaints_path, notice: "Complaint was successfully created & Your Complaint Reference Id is #{@complaint.complaint_id}" 

我已经更改了@complaint/complaint_id 的颜色

<% flash.each do |name, msg| %>
  <div class="alert alert-<%= name == :notice ? "success" : "danger" %>">
   <span class="close" data-dismiss="alert">&times;</span>
    <%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
  </div>
<% end %>

它将以绿色显示成功消息。但我只更改了complaint_id 红色.. 请帮帮我..

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap


    【解决方案1】:

    您可以创建一个帮助器来处理 Flash 消息:

    module ApplicationHelper
    
      def bootstrap_class_for flash_type
        { success: "alert-success", error: "alert-danger", alert: "alert-warning", notice: "alert-info" }[flash_type] || flash_type.to_s
      end
    
      def flash_messages(opts = {})
        flash.each do |msg_type, message|
          concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} fade in") do 
                  concat content_tag(:button, 'x', class: "close", data: { dismiss: 'alert' })
                  concat message 
                end)
        end
        nil
      end
    end
    

    然后在你的 application.html.erb 布局中使用它们:

    <body>
        <div class="container">
    
          <%= flash_messages %>
    
          <%= yield %>
        </div><!-- /container -->
    </body>
    

    如果您不想要这种 DRY 方法,您可以根据自己的需要进行调整。

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2011-01-21
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多