[英]bootstrap flash doesn't work with rails 4.1.4 and simple_form
I'm having difficulties getting flashes to work with bootstrap_flash helper. 我在让Flashs与bootstrap_flash helper一起使用时遇到困难。 Here's a snippet of my code:
这是我的代码片段:
application.html.erb application.html.erb
...
<div class="container">
<%= bootstrap_flash %>
<%= yield %>
</div>
...
bootstrap_flash_helper.rb 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("×"), :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
and I'm calling flash[:notice] in my controller actions. 我在控制器操作中调用flash [:notice]。
Can somebody give me a hint on this one? 有人可以给我一个提示吗? Thanks!
谢谢!
On a first glance the last end
in your code is one too many. 乍一看,您的代码的最后
end
太多了。
On a second glance there are some flaws in your code in my opinion: 乍一看,我认为您的代码中存在一些缺陷:
(1) Avoid casting back and forth: (1)避免来回投射:
type = type.to_sym
type = :success if type.to_s == :notice.to_s
type = :error if type.to_s == :alert.to_s
You're casting type
to a symbol, only to cast it back to a string while casting a constant symbol to a string to compare them. 您将
type
转换为符号,只是将其转换回字符串,而将常量符号转换为字符串以进行比较。 If you omit the to_s
calls you can achieve the same thing without having to cast: 如果省略
to_s
调用,则无需to_s
即可实现相同的目的:
type = type.to_sym
type = :success if type == :notice
type = :error if type == :alert
(2) Use map
instead of a helper variable + each
: (2)使用
map
而不是helper变量+ each
:
flash_messages = []
flash.each do |type, message|
# ...
end
flash_messages.join("\n")
Instead of creating a temporary variable to turn the hash into an array, you can use Enumerable
's map
or collect
method to create a new array: 您可以使用
Enumerable
的map
或collect
方法创建一个新数组,而不是创建一个临时变量以将哈希表转换为数组:
flash.map do |type, message|
# ...
end.join("\n")
(3) Use a mappings hash to map to CSS classes: (3)使用映射哈希映射到CSS类:
ALERT_TYPES = {
:alert => :error, :info => :info, :notice => :success, :warning => :warning
}
By using a hash like this you can simply do a lookup for the match, instead of having several if
statements to determine the correct class 通过使用这样的哈希,您可以简单地查找匹配项,而不用使用多个
if
语句来确定正确的类
content_tag(:div, message, class: "alert alert-#{ALERT_TYPES[type.to_sym]}")
Overall, this would be a far more readable, shorter and expandable solution, I think: 总的来说,我认为这将是一个更具可读性,更简短和可扩展的解决方案:
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("×"), 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.