0
0

More than 3 years have passed since last update.

[Rails]errorsメソッドでメッセージ表示させるとビューが崩れる時の対処

Posted at

はじめに

formで入力に誤り(例えば無入力)があるとエラーメッセージを表示させるためにerrorsメソッドを用いて記述し、動きを確認するとビューが崩れるという問題が発生しました。その時の自分の解決した方法です。

原因

崩れたビューを確認するとfield_with_errorsというdivクラスが付与させていていることを確認した。これが原因である。

対処

今回はこのfield_with_errorsというdivクラスが付与されないようにした。

config/application.rbに以下の記述をした。


module #モデル名
  class Application < Rails::Application

    config.load_defaults 6.0
    config.i18n.default_locale = :ja

    config.action_view.field_error_proc = Proc.new { |html_tag, instance| html_tag }
    #↑この記述をプラスした

  end
end

こうすることで余計なクラスが付与されずにできました!

参考

https://yukimasablog.com/rails-field-with-errors

0
0
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
0
0