2014-03-13 56 views
3

我在引導程序flash消息中遇到問題。我必須更改閃光消息中某些文字的顏色。如何在rails 4中修改引導程序flash消息

控制器

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

我有@投訴/ 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單獨紅色.. 請幫助我..

回答

1

您可以創建一個幫手來處理的提示信息:

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> 

如果您不想採用這種幹法,您可以根據自己的需要進行調整。