<% flash.each do |type, message| %> | |
<div class="alert <%= bootstrap_class_for(type) %> fade in"> | |
<button class="close" data-dismiss="alert">×</button> | |
<%= message %> | |
</div> | |
<% end %> |
<%= render partial: "shared/flash_messages", flash: flash %> |
module ApplicationHelper | |
def bootstrap_class_for flash_type | |
case flash_type | |
when :success | |
"alert-success" | |
when :error | |
"alert-error" | |
when :alert | |
"alert-block" | |
when :notice | |
"alert-info" | |
else | |
flash_type.to_s | |
end | |
end | |
end |
This comment has been minimized.
This comment has been minimized.
@icem +1 |
This comment has been minimized.
This comment has been minimized.
this worked well, thanks! |
This comment has been minimized.
This comment has been minimized.
Which version of Bootstrap is this for? I installed it on BS3 and it won't clear when the "X" is clicked. |
This comment has been minimized.
This comment has been minimized.
Hi, I think there is an issue with Rails 4. May be caused for Turbolinks use? |
This comment has been minimized.
This comment has been minimized.
Nice , it worked for me Rails 4. Thanks |
This comment has been minimized.
This comment has been minimized.
Great ! It worked with rails4. |
This comment has been minimized.
This comment has been minimized.
Great ! It worked with rails4. |
This comment has been minimized.
This comment has been minimized.
Nice one, I've created another version of this here: https://gist.github.com/suryart/7418454 |
This comment has been minimized.
This comment has been minimized.
Great, fixed some problems with flash messages in ruby.railstutorial.org (with Rails 4 and las bootstrap). Thank you. |
This comment has been minimized.
This comment has been minimized.
If you are using Bootstrap 3, the class names have been changed. Here is the new mapping:
|
This comment has been minimized.
This comment has been minimized.
In file _messages.html.erb you can simply put <% flash.each do |type, message| %>
<div class="alert <%= bootstrap_class_for(type) %> alert-dismissable fade in">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<%= message %>
</div>
<% end %> |
This comment has been minimized.
This comment has been minimized.
does this work with GEM --- https://github.com/seyhunak/twitter-bootstrap-rails . I can't get success error message to show. any ideas? |
This comment has been minimized.
This comment has been minimized.
RE: success message not working.... what is strange is the other 3 do work. |
This comment has been minimized.
This comment has been minimized.
FYI - to get this to work, i also needed to add: add_flash_types :success, :info, :warning, :danger |
This comment has been minimized.
This comment has been minimized.
As of 4.1 this code needs to be updated due to the :symbol to String change in flash messages. def bootstrap_class_for(flash_type)
case flash_type
when "success"
"alert-success" # Green
when "error"
"alert-danger" # Red
when "alert"
"alert-warning" # Yellow
when "notice"
"alert-info" # Blue
else
flash_type.to_s
end
end |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
def alert_class_for(flash_type)
{
:success => 'alert-success',
:error => 'alert-danger',
:alert => 'alert-warning',
:notice => 'alert-info'
}[flash_type.to_sym] || flash_type.to_s
end |
This comment has been minimized.
This comment has been minimized.
Thanks for the great tip @annieogrady, that worked! |
This comment has been minimized.
This comment has been minimized.
For Bootstrap 3: <% flash.each do |type, message| %>
<div class="alert <%= alert_class_for(type) %> alert-dismissible fade in">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<%= message %>
</div>
<% end %> |
This comment has been minimized.
This comment has been minimized.
Thanks! This worked great for my Rails 4 app. |
This comment has been minimized.
This comment has been minimized.
@icem +1, @JoshTGreenwood +1, @rafaelfragosom +1 |
This comment has been minimized.
This comment has been minimized.
The code with the correct message shows up in the source but does not display on the screen. |
This comment has been minimized.
This comment has been minimized.
Rails 4.1 my partial is always rendering with classes 'alert alert'. My testimonials_controller: Everything works fine with :alert, but not with any of the other names. The symbol doesn't seem to be getting recognised by the helper method. |
This comment has been minimized.
This comment has been minimized.
fantastic, thank you! |
This comment has been minimized.
This comment has been minimized.
This works better in Bootstrap 3 IMHO. |
This comment has been minimized.
This comment has been minimized.
for those that just need 2 colours for simplicity
- flash.each do |name, msg|
%div{:class => "alert alert-#{name.to_s == 'notice' ? "success" : "warning"}"}
%a.close{"data-dismiss" => "alert"} ×
%div{:id => "flash_#{name}"}= msg module FlashHelper
def flash_messages
render partial: "layouts/flash_messages", flash: flash
end
end |
This comment has been minimized.
This comment has been minimized.
Easy! |
This comment has been minimized.
Why not just using Hash for #bootstrap_class_for ?