LoginSignup
40
51

More than 5 years have passed since last update.

devise専用エラーをbootstrap3のエラーと同様に表示する

Last updated at Posted at 2014-09-15

やりたいこと

devise専用のエラー表示(devise_error_messages!)を、
他のalertと同じようにBootstrap3を利用した表示方法に置き換えたい。

devise.jpg

 ▽ ▽ ▽ ▽ ▽ ▽ ▽ ▽ ▽ ▽ ▽ ▽ ▽

bootstrap3.jpg

実装方法

deviseのヘルパーを作って"devise_error_messages!"をオーバーライドした箇所で整形。
あまりスマートではないかもしれませんが、これが一番簡単そうですね…。

app/helpers/devise_helper.rb
module DeviseHelper
  def devise_error_messages!
    return "" if resource.errors.empty?

    html = ""
    messages = resource.errors.full_messages.each do |errmsg|
      html += <<-EOF
      <div class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert">
          <span aria-hidden="true">&times;</span>
          <span class="sr-only">close</span>
        </button>
        #{errmsg}
      </div>
      EOF
    end
    html.html_safe
  end

  def devise_error_messages?
    resource.errors.empty? ? false : true
  end

end
40
51
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
51