Merge pull request #39825 from manuelmeurer/improve-active-record-validations-docs

improve active record validation docs [ci skip]
This commit is contained in:
Eugene Kenny 2020-08-03 14:13:56 +01:00 committed by GitHub
commit 7c32f09c4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 11 deletions

View File

@ -558,8 +558,8 @@ Since `false.blank?` is true, if you want to validate the presence of a boolean
field you should use one of the following validations:
```ruby
validates :boolean_field_name, inclusion: { in: [true, false] }
validates :boolean_field_name, exclusion: { in: [nil] }
validates :boolean_field_name, inclusion: [true, false]
validates :boolean_field_name, exclusion: [nil]
```
By using one of these validations, you will ensure the value will NOT be `nil`
@ -684,7 +684,7 @@ validator class as `options`:
```ruby
class GoodnessValidator < ActiveModel::Validator
def validate(record)
if options[:fields].any?{|field| record.send(field) == "Evil" }
if options[:fields].any? { |field| record.send(field) == "Evil" }
record.errors.add :base, "This person is evil"
end
end
@ -799,9 +799,9 @@ class Person < ApplicationRecord
# Hard-coded message
validates :name, presence: { message: "must be given please" }
# Message with dynamic attribute value. %{value} will be replaced with
# the actual value of the attribute. %{attribute} and %{model} also
# available.
# Message with dynamic attribute value. %{value} will be replaced
# with the actual value of the attribute. %{attribute} and %{model}
# are also available.
validates :age, numericality: { message: "%{value} seems wrong" }
# Proc
@ -810,7 +810,7 @@ class Person < ApplicationRecord
# object = person object being validated
# data = { model: "Person", attribute: "Username", value: <username> }
message: ->(object, data) do
"Hey #{object.name}!, #{data[:value]} is taken already! Try again #{Time.zone.tomorrow}"
"Hey #{object.name}, #{data[:value]} is already taken."
end
}
end
@ -1260,9 +1260,9 @@ Assuming we have a model that's been saved in an instance variable named
<h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% @article.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
<% @article.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
@ -1274,7 +1274,7 @@ the entry.
```html
<div class="field_with_errors">
<input id="article_title" name="article[title]" size="30" type="text" value="">
<input id="article_title" name="article[title]" size="30" type="text" value="">
</div>
```