LoginSignup
1
0

More than 5 years have passed since last update.

rails エラーオブジェクト のデフォルトメッセージを書き換える

Posted at

初心者です。記念すべき1記事目です。
バリデーションのテストコードを書いていたのですが、ふと、どうやったら元々のエラーメッセージを変えれるんやと思いまして、色々調べたところ以下の書き方で上手く行ったので共有しておきます。

 最初

元々のエラー文を前提としたテストコード

it "is invalid if not having uniqe email" do
      user = create(:user)
      user2 = build(:user)
      user2.valid?
      expect(user2.errors[:email]).to include("has already been taken")
    end
    it "is invalid without a password" do
      user = build(:user, password: "", password_confirmation: "")
      user.valid?
      expect(user.errors[:password]).to include("can't be blank")
    end

テスト結果

スクリーンショット 2018-05-03 11.04.59.png

en.ymlを書き換える

en:
  activerecord:
    errors:
      messages:
        blank: "what the fuck!!! You need to fill it"
        taken: "what the hell are you thinking? it already exists"
  hello: "Hello world"

テスト結果

スクリーンショット 2018-05-03 11.07.13.png

以上です

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