未验证 提交 7c32f09c 编写于 作者: E Eugene Kenny 提交者: GitHub

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

improve active record validation docs [ci skip]
......@@ -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>
```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册