Ruby on rails 引导闪存不';t使用rails 4.1.4和简单表单

Ruby on rails 引导闪存不';t使用rails 4.1.4和简单表单,ruby-on-rails,twitter-bootstrap,ruby-on-rails-4,twitter-bootstrap-3,Ruby On Rails,Twitter Bootstrap,Ruby On Rails 4,Twitter Bootstrap 3,我很难让flash与bootstrap\u flash helper一起工作。 以下是我的代码片段: application.html.erb ... <div class="container"> <%= bootstrap_flash %> <%= yield %> </div> ... 。。。 ... bootstrap\u flash\u helper.rb ALERT_TYPES = [:error, :info, :su

我很难让flash与bootstrap\u flash helper一起工作。 以下是我的代码片段:

application.html.erb

...
<div class="container">
  <%= bootstrap_flash  %>
  <%= yield %> 
</div>
...
。。。
...
bootstrap\u flash\u helper.rb

ALERT_TYPES = [:error, :info, :success, :warning] unless const_defined?(:ALERT_TYPES)

def bootstrap_flash
  flash_messages = []
  flash.each do |type, message|
    # Skip empty messages, e.g. for devise messages set to nothing in a locale file.
    next if message.blank?

    type = type.to_sym
    type = :success if type.to_s == :notice.to_s
    type = :error   if type.to_s == :alert.to_s
    next unless ALERT_TYPES.include?(type)

    Array(message).each do |msg|
      text = content_tag(:div, content_tag(:button, raw("&times;"), :class => "close", "data-dismiss" => "alert") + msg.html_safe, :class => "alert fade in alert-#{type}")
      flash_messages << text if msg
    end
  end
    flash_messages.join("\n").html_safe
end
end   
ALERT_TYPES=[:error,:info,:success,:warning],除非定义了常量(:ALERT_TYPES)
def引导闪存
flash_消息=[]
闪存。每个do |类型、消息|
#跳过空消息,例如,对于区域设置文件中设置为“无”的设计消息。
下一个if message.blank?
type=type.to_sym
type=:如果type.to\u s==:notice.to\u s成功
类型=:如果type.to\u s==:alert.to\u s,则出现错误
下一个警报类型。包括?(类型)
数组(消息)。每个do | msg|
text=content_标记(:div,content_标记(:button,raw(“×;”),:class=>“close”,“data disease”=>“alert”)+msg.html_safe,:class=>“alert淡入警报-#{type}”)

flash_消息第一眼看到代码中的最后一个
end
太多了

再看一眼,我认为您的代码中存在一些缺陷:

(1)避免来回投掷:

type = type.to_sym
type = :success if type.to_s == :notice.to_s
type = :error   if type.to_s == :alert.to_s
flash_messages = []
flash.each do |type, message|
  # ...
end
flash_messages.join("\n")
ALERT_TYPES = {
 :alert => :error, :info => :info, :notice => :success, :warning => :warning
}
您正在将
类型
强制转换为一个符号,但在将常量符号强制转换为字符串以进行比较时,却将其强制转换回字符串。如果省略
to_s
调用,则无需强制转换即可实现相同的效果:

type = type.to_sym
type = :success if type == :notice
type = :error   if type == :alert
(2)使用
映射
而不是辅助变量+
每个

type = type.to_sym
type = :success if type.to_s == :notice.to_s
type = :error   if type.to_s == :alert.to_s
flash_messages = []
flash.each do |type, message|
  # ...
end
flash_messages.join("\n")
ALERT_TYPES = {
 :alert => :error, :info => :info, :notice => :success, :warning => :warning
}
您可以使用
Enumerable
或方法创建新数组,而不是创建临时变量将哈希转换为数组:

flash.map do |type, message|
  # ...
end.join("\n")
(3)使用映射哈希映射到CSS类:

type = type.to_sym
type = :success if type.to_s == :notice.to_s
type = :error   if type.to_s == :alert.to_s
flash_messages = []
flash.each do |type, message|
  # ...
end
flash_messages.join("\n")
ALERT_TYPES = {
 :alert => :error, :info => :info, :notice => :success, :warning => :warning
}
通过使用这样的散列,您可以简单地查找匹配项,而不是使用几个
if
语句来确定正确的类

content_tag(:div, message, class: "alert alert-#{ALERT_TYPES[type.to_sym]}")
总的来说,这将是一个更具可读性、更短和可扩展性的解决方案,我认为:

ALERT_TYPES = { :alert => :error, :info => :info, :notice => :success, :warning => :warning } unless const_defined?(:ALERT_TYPES)

def bootstrap_flash
  flash.map do |type, message|
    # Skip empty messages, e.g. for devise messages set to nothing in a locale file.
    # This will leave a nil value in the resulting array
    next if message.blank?

    # Skip if it's not a valid alert type
    # This will leave a nil value in the resulting array
    type = type.to_sym
    next unless ALERT_TYPES.keys.include?(type)

    # return the markup for the alert
    content_tag(:div, class: "alert alert-#{ALERT_TYPES[type]} fade in") do
      # use safe_concat() to avoid using html_safe()
      content_tag(:button, raw("&times;"), class: "close", data: { dismiss: "alert" }).safe_concat(message)
    end
  end.join('') # no need to join with \n --> this way nil values will be ignored as well
end

它在这里崩溃:def bootstrap_flash flash_messages=[]flash.each do | type,message | next if message.blank?RAISE准确的错误信息是什么?我认为最后一个
结尾
太多了。。。