Last Updated: February 25, 2016
·
2.531K
· earnestaddae

Devise v3 Error Messages! Bootstrap 3 style

I am really new to Ruby on Rails and finding the answer to the problem above was a bit tricky, but I finally got it. I'm using Rails 4. This is just an update of Piere Louis Gottfrois' similar article.

Create file named:


/app/helpers/devise_helper.rb


Add the following code:


module DeviseHelper
  def devise_error_messages!
  return '' if resource.errors.empty?

  messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
  sentence = I18n.t('errors.messages.not_saved',
  count: resource.errors.count,
  resource: resource.class.model_name.human.downcase)

  # for Bootstrap 3 pay attention to the classes in the "div"

  html = <<-HTML
  <div class="alert alert-warning alert-dismissable"> 
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
  <h4>#{sentence}</h4>
  #{messages}</div>
  HTML
  html.html_safe
 end
end