diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2015-03-25 09:06:49 +0100 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2015-03-25 09:06:49 +0100 |
commit | 0f76ea63e12c1f2a251b9eee9189c26a09c97b69 (patch) | |
tree | 2cd0a3574f36d7a8a8109a70cff28d59b1887215 /guides/source | |
parent | 5c0266dd1db34758f5695a44fb6eb6e36bb5d888 (diff) | |
parent | 616f9a8fdb76f2c3becad43abcd2a90449d7bf3f (diff) | |
download | rails-0f76ea63e12c1f2a251b9eee9189c26a09c97b69.tar.gz rails-0f76ea63e12c1f2a251b9eee9189c26a09c97b69.tar.bz2 rails-0f76ea63e12c1f2a251b9eee9189c26a09c97b69.zip |
Merge pull request #19507 from jonatack/ar-validations-guide-fixes
[skip ci] Active Record Validations guide fixes
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/active_record_validations.md | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md index f19934fe89..d251c5c0b1 100644 --- a/guides/source/active_record_validations.md +++ b/guides/source/active_record_validations.md @@ -1055,7 +1055,9 @@ person.errors[:name] ### `errors.add` -The `add` method lets you manually add messages that are related to particular attributes. You can use the `errors.full_messages` or `errors.to_a` methods to view the messages in the form they might be displayed to a user. Those particular messages get the attribute name prepended (and capitalized). `add` receives the name of the attribute you want to add the message to, and the message itself. +The `add` method lets you add an error message related to a particular attribute. It takes as arguments the attribute and the error message. + +The `errors.full_messages` method (or its equivalent, `errors.to_a`) returns the error messages in a user-friendly format, with the capitalized attribute name prepended to each message, as shown in the examples below. ```ruby class Person < ActiveRecord::Base @@ -1073,12 +1075,12 @@ person.errors.full_messages # => ["Name cannot contain the characters !@#%*()_-+="] ``` -Another way to do this is using `[]=` setter +An equivalent to `errors#add` is to use `<<` to append a message to the `errors.messages` array for an attribute: ```ruby class Person < ActiveRecord::Base def a_method_used_for_validation_purposes - errors.add(:name, "cannot contain the characters !@#%*()_-+=") + errors.messages[:name] << "cannot contain the characters !@#%*()_-+=" end end |