Any idea why the messages hash would be empty in this situation?
I have these validations:
validates_presence_of :part_number
validates_uniqueness_of :part_number
a simple create:
if #part.save
puts 'saved'
redirect_to new_v2_path
else
puts 'not saved'
flash[:error] = "There was an error while updating the part."
redirect_to new_v2_path(#part)
end
in the view:
<% if #part.errors.any? %>
<div id="error_explanation" style="color: red;">
<h2><%= pluralize(#part.errors.count, 'error') %> :</h2>
<ul>
<% #part.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
the error object:
...l, flush_part: nil>, #messages={}>
In your failed validation branch you are redirecting instead of rendering, generating a new request to :new_2 action and instantiating a new #part object, with no value assigned and thus errors. You need to use render instead.
Change this line:
redirect_to new_v2_path(#part)
To this one:
render :new # or new_v2 or whatever action name you have.
I have this code:
<% flash.each do |type, message| %>
<div class="alert <%= flash_class type %>">
<button class="close" data-dismiss="alert">x</button>
<%= message + " " + type %>
</div>
<% end %>
to display flash messages, and in the application_helper I have this:
module ApplicationHelper
def flash_class(type)
case type
when :alert
"alert-danger"
when :notice
"alert-success"
else
"alert-info"
end
end
end
If I intentionally do this <div class="alert <%= flash_class :alert %>"> it displays with the proper colour (result of the correct class in the html). Otherwise I always get this result:
I'm new at ruby-on-rails so I must have missed something simple, but I don't know what. Any help?
I'm using ruby 2.1.2 and rails 4.1.5.
The flash type has changed in (I think) Rails 4, it's a string, you're comparing symbols, so it'll never match.
Try this, it'll handle both:
def flash_class(type)
case type
when :alert, 'alert'
"alert-danger"
when :notice, 'notice'
"alert-success"
else
"alert-info"
end
end
I'm trying to make rails' flash messages style with bootstrap 3.
In this piece of code,
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<%=key> puts the key into the class tag like you'd expect. But when I change the middle line to this
<div class="<%= flash_class(key) %>"><%= value %></div>
<%= flash_class(key)%> doesn't embed anything.
flash_class() is in application_helper.rb which is automatically included in views (right?) and it returns a string. I think it's probably something stupid I'm missing, why does this not work?
edit- here's the implementation of flash_class
def flash_class(level)
case level
when :notice then "alert alert-info"
when :success then "alert alert-success"
when :error then "alert alert-error"
when :alert then "alert alert-error"
end
end
Probably a problem comparing string with symbols:
def flash_class(level)
case level.to_sym
Should solve your problem
I am trying to use Bootstrap styles to Display the Flash messages with color to the users.
Controller
def create
#category = Category.new(category_params)
# Assigning default values
#category.status = 1.to_i
if #category.save
redirect_to admin_categories_path, :flash => { :success => "Category was successfully created." }
else
flash[:error] = "Category could not be save. Please try again."
render :new
end
end
View
<%= render 'admin/partials/flash_message' %>
Partial
<% if flash.present? %>
<%= flash.inspect %>
<% flash.each do |key, value| %>
<div class="<%= flash_class(key) %> fade in widget-inner">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= value %>
</div>
<% end %>
<% end %>
Helper
# Flash Messages
def flash_class(level)
case level
when :notice then "alert alert-info"
when :success then "alert alert-success"
when :error then "alert alert-error"
when :alert then "alert alert-error"
end
end
Output
I am not able to pass the key to the helper function. I am assuming it sends blank.
This is the HTML that gets outputted
<div class=" fade in widget-inner">
<button data-dismiss="alert" class="close" type="button">×</button>
Category could not be save. Please try again.
</div>
I am not able to figure out why foreach is not able to extract the key and value pair.
on inspect i get the following
#<ActionDispatch::Flash::FlashHash:0x007fc0e74dfa58 #discard=#<Set: {}>, #flashes={"error"=>"Category could not be save. Please try again."}, #now=nil>
in Rails 4, it supports only strings. So i had to change the Helper to the following
# Flash Messages
def flash_class(level)
case level
when 'notice' then "alert alert-info"
when 'success' then "alert alert-success"
when 'error' then "alert alert-danger"
when 'alert' then "alert alert-warning"
end
end
**Edit: According to Deep Suggestions **
By Changing the Flash Partial to
<% if flash.present? %>
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %> fade in widget-inner">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= value %>
</div>
<% end %>
<% end %>
And by Just Extendting the BootStrap Classes as follows we can eliminate the Helper class all together.
alert-notice { #extend .alert-info; }
alert-error { #extend .alert-danger; }
I am integrating twitter bootstrap css into my application. Going along just fine,but I don't know how to customize the css and wrappers for my flash messages.
I would like my flash messages to be formatted with the default Bootstrap classes:
<div class="alert-message error">
<a class="close" href="#">×</a>
<p><strong>Oh snap!</strong> Change this and that and try again.</p>
</div>
Currently I output my flash messages with:
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
Is there an easy way to run a little switch that would make :notification or other rails flash messages map to the classes in bootcamp, like info?
My answer for Bootstrap 2.0 starts from the helpful answer by #Railslerner but uses different code in the partial.
app/helpers/application_helper.rb (same as #Railslerner's answer)
module ApplicationHelper
def flash_class(level)
case level.to_sym
when :notice then "alert alert-info"
when :success then "alert alert-success"
when :error then "alert alert-error"
when :alert then "alert alert-error"
end
end
end
Somewhere in app/views/layouts/application.html.erb:
<%= render 'layouts/flash_messages' %>
app/views/layouts/_flash_messages.html.erb
<div>
<% flash.each do |key, value| %>
<div class="<%= flash_class(key) %> fade in">
×
<%= value %>
</div>
<% end %>
</div>
Differences:
Does not loop through different error levels each time it is called.
Instead loops through the flash hash if the hash contains messages (following the
approach in Michael Hartl's Rails Tutorial).
Does not use the <p> tag, no longer required in Bootstrap 2.0.
Remember to include bootstrap-alert.js so the fade and close functionality will work. If you're using the bootstap-sass gem, add this line to app/assets/javascripts/application.js:
//= require bootstrap-alert
Update 8/9/2012: Folders updated. I actually put everything except the helper under app/views/layouts since flash_messages is only used in app/views/layouts/application.html.erb.
Update 6/5/2015: After updating to Rails 4.2, I discovered that level was (at least sometimes) coming in as a String and failing to match the case statement in the ApplicationHelper. Changed that to level.to_sym.
Here's my answer with Bootstrap 2.0.0
app/helpers/application_helper.rb
module ApplicationHelper
def flash_class(level)
case level
when :notice then "alert alert-info"
when :success then "alert alert-success"
when :error then "alert alert-error"
when :alert then "alert alert-error"
end
end
end
app/views/shared/_flash_messages.html.erb
<% [:notice, :error, :alert].each do |level| %>
<% unless flash[level].blank? %>
<div class="<%= flash_class(level) %> fade in">
×
<%= content_tag :p, flash[level] %>
</div>
<% end %>
<% end %>
This gives you the fade out when closed and close button. If you were using HAML, checkout this guys post: http://ruby.zigzo.com/2011/10/02/flash-messages-twitters-bootstrap-css-framework/
I am adding a new answer for Bootstrap 3.0 based on Mark Berry's answer. The Bootstrap CSS for alerts is at http://getbootstrap.com/components/#alerts
app/helpers/application_helper.rb
module ApplicationHelper
def flash_class(level)
case level
when :notice then "alert-info"
when :success then "alert-success"
when :error then "alert-danger"
when :alert then "alert-warning"
end
end
end
app/views/layouts/_flash_messages.html.erb
<div>
<% flash.each do |key, value| %>
<div class="alert alert-dismissable <%= flash_class(key) %> fade in">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<%= value %>
</div>
<% end %>
</div>
Differences:
Change Bootstrap classes for error and alert.
Add .alert-dismissable and change the code for close button.
Try this:
application_helper.rb
def flash_class(level)
case level
when :notice then "info"
when :error then "error"
when :alert then "warning"
end
end
and then
<% [:notice, :error, :alert].each do |level| %>
<% unless flash[level].blank? %>
<div data-alert="alert" class="alert-message <%= flash_class(level) %> fade in">
<a class="close" href="#">×</a>
<%= content_tag :p, flash[level] %>
</div>
<% end %>
<% end %>
Bootstrap 3 class names (adjusted from Mark Berry's answer):
def flash_class(level)
case level
when :notice then "alert alert-info"
when :success then "alert alert-success"
when :error then "alert alert-danger"
when :alert then "alert alert-warning"
end
end
I would suggest adding classes for the different notification levels used in rails:
.alert-notice {
background-color: #f2dede;
border-color: #eed3d7;
color: #b94a48; }
etc.
and use them according to the examples from twitter bootstrap:
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>">
×
<%= value %>
</div>
<% end %>
This makes a ApplicationHelper#flash_class(level) obsolete. It hardcodes styling into the application, which smells. Styling belongs into stylesheets.
It's not the ideal solution, but assuming you only ever use 'notice' or 'error' flash messages, you can use this:
...
<% content_tag :div, :id => "flash_#{name}", :class => "alert-message #{name == "notice" ? "success" : name}" do %>
...
If you want to completely alter the styling of the flash message--for example, if you don't want the message to fade, you can even do something like this:
In your controller:
flash[:order_error] = "This is an important error that shouldn't fade!"
Then compare the flash key to show the appropriate styling (with to_sym):
<% flash.each do |key, msg| %>
<% if key == 'order_error'.to_sym %>
<div class="error" id="newErrorStyle"><%= msg %></div>
<% else %>
<div class="<%= key %>" id="flash-<%= key %>"><%= msg %></div>
<% content_tag :script, :type => "text/javascript" do -%>
setTimeout("new Effect.Fade('flash-<%= key %>');", 8000);
<% end %>
<% end %>
<% end %>