【问题标题】:bootstrap flash doesn't work with rails 4.1.4 and simple_formbootstrap flash 不适用于 rails 4.1.4 和 simple_form
【发布时间】:2014-07-18 15:15:02
【问题描述】:

我在让 flash 与 bootstrap_flash helper 一起工作时遇到了困难。 这是我的代码的 sn-p:

application.html.erb

...
<div class="container">
  <%= bootstrap_flash  %>
  <%= yield %> 
</div>
...

bootstrap_flash_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   

我在控制器操作中调用 flash[:notice]。

有人可以给我一个提示吗? 谢谢!

【问题讨论】:

  • 它在这里崩溃了:def bootstrap_flash flash_messages = [] flash.each do |type, message|接下来如果message.blank?提高
  • 确切的错误信息是什么?我认为最后一个end 太多了...

标签: ruby-on-rails twitter-bootstrap ruby-on-rails-4 twitter-bootstrap-3


【解决方案1】:

乍一看,代码中的最后一个end 太多了。

在我看来,你的代码有一些缺陷:

(1) 避免来回施法:

type = type.to_sym
type = :success if type.to_s == :notice.to_s
type = :error   if type.to_s == :alert.to_s

您将type 转换为符号,只是将其转换回字符串,同时将常量符号转换为字符串以进行比较。如果您省略 to_s 调用,则无需强制转换即可实现相同的目标:

type = type.to_sym
type = :success if type == :notice
type = :error   if type == :alert

(2) 使用map代替辅助变量+each

flash_messages = []
flash.each do |type, message|
  # ...
end
flash_messages.join("\n")

您可以使用Enumerablemapcollect方法创建一个新数组,而不是创建一个临时变量来将哈希转换为数组:

flash.map do |type, message|
  # ...
end.join("\n")

(3) 使用映射散列映射到 CSS 类:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 2014-02-17
    • 1970-01-01
    相关资源
    最近更新 更多