LoginSignup
0
1

More than 3 years have passed since last update.

エラーメッセージ

Posted at

errorsメソッド

【例】app/views/groups/の中に_form.html.hamlファイルを作成したとします

app/views/groups/_form.html.haml
= form_for group do |f|
  - if group.errors.any?
    .chat-group-form__errors
      %h2= "#{group.errors.full_messages.count}件のエラーが発生しました。"
      %ul
        - group.errors.full_messages.each do |message|
          %li= message
                      〜省略〜

group.saveのように変数groupの保存をしようとした時に、失敗するとgroupにエラーメッセージが格納されます。
その変数に対してerrorメソッドを使用するとエラーメッセージの取得ができます。
any?メソッドによって、エラーメッセージの有無を判定しています。
full_messagesメソッドによってエラーメッセージ全件の内容を取得しています。

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