diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-01-31 14:10:23 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-01-31 14:10:23 -0800 |
commit | 6b4a5952d3952ffc98173626e8d76e73e6140e3a (patch) | |
tree | d7f66e9a07e32e15a33035688fc65e0b00a0cefa | |
parent | dd53c0679d307378cfd7fadc67a5c180f2a61562 (diff) | |
parent | 9a6c6c6f094ce965cc251865bdc1828bc4f38039 (diff) | |
download | rails-6b4a5952d3952ffc98173626e8d76e73e6140e3a.tar.gz rails-6b4a5952d3952ffc98173626e8d76e73e6140e3a.tar.bz2 rails-6b4a5952d3952ffc98173626e8d76e73e6140e3a.zip |
Merge pull request #18700 from nygrenh/better-required-message
Provide a better error message on :required association
4 files changed, 29 insertions, 3 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 5830a00bc5..4c78fc91a5 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,8 @@ +* Change the default error message from `can't be blank` to `must exist` for + the presence validator of the `:required` option on `belongs_to`/`has_one` associations. + + *Henrik Nygren* + * Assigning an unknown attribute key to an `ActiveModel` instance during initialization will now raise `ActiveModel::AttributeAssignment::UnknownAttributeError` instead of `NoMethodError` diff --git a/activemodel/lib/active_model/locale/en.yml b/activemodel/lib/active_model/locale/en.yml index bf07945fe1..b0b20ee0bf 100644 --- a/activemodel/lib/active_model/locale/en.yml +++ b/activemodel/lib/active_model/locale/en.yml @@ -14,6 +14,7 @@ en: empty: "can't be empty" blank: "can't be blank" present: "must be blank" + required: "must exist" too_long: one: "is too long (maximum is 1 character)" other: "is too long (maximum is %{count} characters)" diff --git a/activerecord/lib/active_record/associations/builder/singular_association.rb b/activerecord/lib/active_record/associations/builder/singular_association.rb index 1369212837..f6274c027e 100644 --- a/activerecord/lib/active_record/associations/builder/singular_association.rb +++ b/activerecord/lib/active_record/associations/builder/singular_association.rb @@ -31,7 +31,7 @@ module ActiveRecord::Associations::Builder def self.define_validations(model, reflection) super if reflection.options[:required] - model.validates_presence_of reflection.name + model.validates_presence_of reflection.name, message: :required end end end diff --git a/activerecord/test/cases/associations/required_test.rb b/activerecord/test/cases/associations/required_test.rb index a6123ac432..8b765a2e0c 100644 --- a/activerecord/test/cases/associations/required_test.rb +++ b/activerecord/test/cases/associations/required_test.rb @@ -40,7 +40,7 @@ class RequiredAssociationsTest < ActiveRecord::TestCase record = model.new assert_not record.save - assert_equal ["Parent can't be blank"], record.errors.full_messages + assert_equal ["Parent must exist"], record.errors.full_messages record.parent = Parent.new assert record.save @@ -64,12 +64,32 @@ class RequiredAssociationsTest < ActiveRecord::TestCase record = model.new assert_not record.save - assert_equal ["Child can't be blank"], record.errors.full_messages + assert_equal ["Child must exist"], record.errors.full_messages record.child = Child.new assert record.save end + test "required has_one associations have a correct error message" do + model = subclass_of(Parent) do + has_one :child, required: true, inverse_of: false, + class_name: "RequiredAssociationsTest::Child" + end + + record = model.create + assert_equal ["Child must exist"], record.errors.full_messages + end + + test "required belongs_to associations have a correct error message" do + model = subclass_of(Child) do + belongs_to :parent, required: true, inverse_of: false, + class_name: "RequiredAssociationsTest::Parent" + end + + record = model.create + assert_equal ["Parent must exist"], record.errors.full_messages + end + private def subclass_of(klass, &block) |