This question already has an answer here:
I am using Rails 4, Twitter Bootstrap 3.0, and Devise 3.0. I am trying to get identical looking flash messages for devise and the rest of my site. So far I have this:
Within "app/views/layouts/application.html.erb":
<%= render 'layouts/messages' %>
"app/views/layouts/_messages.html.erb":
<% devise_flash %>
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>">
<a href="#" data-dismiss="alert" class="close">×</a>
<%= value %>
</div>
<% end %>
I have created a "app/helpers/devise_helper.rb" in order to override the default devise error messages:
module DeviseHelper
def devise_error_messages!
end
end
"app/helpers/application_helper.rb":
def devise_flash
if controller.devise_controller? && resource.errors.any?
flash.now[:error] = flash[:error].to_a.concat resource.errors.full_messages
flash.now[:error].uniq!
end
end
"app/assets/stylesheets/custom.css.scss":
.alert-error {
background-color: #f2dede;
border-color: #eed3d7;
color: #b94a48;
text-align: center; }
.alert-alert {
background-color: #f2dede;
border-color: #eed3d7;
color: #b94a48;
text-align: center; }
.alert-success {
background-color: #dff0d8;
border-color: #d6e9c6;
color: #468847;
text-align: center; }
.alert-notice {
background-color: #dff0d8;
border-color: #d6e9c6;
color: #468847;
text-align: center; }
It displays normal flash messages fine but the issue is when I have a devise error. It looks like this:
I am trying to list the errors more like this:
I don't want the errors to be surrounded by "". It does not need to have the bullets, but I want it to be listed vertically. I think I need to edit my devise_flash method in the "app/helpers/application_helper.rb" file.
How can I do this?
I made a wiki page within the devise wiki on github for How To: Integrate I18n Flash Messages with Devise and Bootstrap
Flash Messages For the Site
First we will make a rendered view to make the code concise. Within "app/views/layouts/application.html.erb" I added
<%= render 'layouts/messages' %>
.My file looks like:
Next we have to make the messages file. Make a new file in "app/views/layouts/_messages.html.erb" and add:
This will give us flash messages for the entire site.
Flash Messages For Devise
For devise you need to override the way devise handles flash messages. Create a file called devise_helper in "app/helpers/devise_helper.rb".
Inside the file you have to create a method called devise_error_messages!, which is the name of the file that tells devise how to handle flash messages.
Next in your devise views you will have to define where you want the error messages to appear. You will need to enter
<%= devise_error_messages! %>
within the devise pages. An example is entering this within "app/views/devise/registrations/.new.html.erb" (The sign up page)The method call should already be within the file, but you can move the code around to customize where it is shown.
CSS For Flash Messages
If you do not want to use the odd blue and yellow alerts that come default, I have set error and alert to have the same color and success and notice to have the same color. I am using red for errors and alerts, and green for success and notice.
Within my "app/assets/stylesheets/custom.css.scss" I have:
I'm guessing since
flash[:error].to_a resource.errors.full_messages
produces an array which is then simply printed out in your alert through<%= value %>
, it prints the entire array which is not what you want.Instead of printing the array directly, you may want to try looping through the contents like so: