mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix Active Record validation error messages markup in guides
The other way it was not marking the text as italic, it was showing the underlines as normal text. Also fixes some code examples indentation and # => marks in Active Model and Active Record guides. [ci skip]
This commit is contained in:
parent
7f395600ce
commit
129eac0243
4 changed files with 61 additions and 61 deletions
|
@ -85,9 +85,9 @@ class Person
|
|||
end
|
||||
|
||||
person = Person.new
|
||||
person.to_model == person #=> true
|
||||
person.to_key #=> nil
|
||||
person.to_param #=> nil
|
||||
person.to_model == person # => true
|
||||
person.to_key # => nil
|
||||
person.to_param # => nil
|
||||
```
|
||||
|
||||
### Dirty
|
||||
|
@ -130,22 +130,22 @@ end
|
|||
|
||||
```ruby
|
||||
person = Person.new
|
||||
person.changed? #=> false
|
||||
person.changed? # => false
|
||||
|
||||
person.first_name = "First Name"
|
||||
person.first_name #=> "First Name"
|
||||
person.first_name # => "First Name"
|
||||
|
||||
# returns if any attribute has changed.
|
||||
person.changed? #=> true
|
||||
person.changed? # => true
|
||||
|
||||
# returns a list of attributes that have changed before saving.
|
||||
person.changed #=> ["first_name"]
|
||||
person.changed # => ["first_name"]
|
||||
|
||||
# returns a hash of the attributes that have changed with their original values.
|
||||
person.changed_attributes #=> {"first_name"=>nil}
|
||||
person.changed_attributes # => {"first_name"=>nil}
|
||||
|
||||
# returns a hash of changes, with the attribute names as the keys, and the values will be an array of the old and new value for that field.
|
||||
person.changes #=> {"first_name"=>[nil, "First Name"]}
|
||||
person.changes # => {"first_name"=>[nil, "First Name"]}
|
||||
```
|
||||
|
||||
#### Attribute based accessor methods
|
||||
|
@ -154,23 +154,23 @@ Track whether the particular attribute has been changed or not.
|
|||
|
||||
```ruby
|
||||
# attr_name_changed?
|
||||
person.first_name #=> "First Name"
|
||||
person.first_name_changed? #=> true
|
||||
person.first_name # => "First Name"
|
||||
person.first_name_changed? # => true
|
||||
```
|
||||
|
||||
Track what was the previous value of the attribute.
|
||||
|
||||
```ruby
|
||||
# attr_name_was accessor
|
||||
person.first_name_was #=> "First Name"
|
||||
person.first_name_was # => "First Name"
|
||||
```
|
||||
|
||||
Track both previous and current value of the changed attribute. Returns an array if changed, else returns nil.
|
||||
|
||||
```ruby
|
||||
# attr_name_change
|
||||
person.first_name_change #=> [nil, "First Name"]
|
||||
person.last_name_change #=> nil
|
||||
person.first_name_change # => [nil, "First Name"]
|
||||
person.last_name_change # => nil
|
||||
```
|
||||
|
||||
### Validations
|
||||
|
@ -189,12 +189,12 @@ class Person
|
|||
end
|
||||
|
||||
person = Person.new(token: "2b1f325")
|
||||
person.valid? #=> false
|
||||
person.valid? # => false
|
||||
person.name = 'vishnu'
|
||||
person.email = 'me'
|
||||
person.valid? #=> false
|
||||
person.valid? # => false
|
||||
person.email = 'me@vishnuatrai.com'
|
||||
person.valid? #=> true
|
||||
person.valid? # => true
|
||||
person.token = nil
|
||||
person.valid? #=> raises ActiveModel::StrictValidationFailed
|
||||
person.valid? # => raises ActiveModel::StrictValidationFailed
|
||||
```
|
||||
|
|
|
@ -147,15 +147,15 @@ Active Record objects can be created from a hash, a block or have their attribut
|
|||
For example, given a model `User` with attributes of `name` and `occupation`, the `create` method call will create and save a new record into the database:
|
||||
|
||||
```ruby
|
||||
user = User.create(name: "David", occupation: "Code Artist")
|
||||
user = User.create(name: "David", occupation: "Code Artist")
|
||||
```
|
||||
|
||||
Using the `new` method, an object can be created without being saved:
|
||||
|
||||
```ruby
|
||||
user = User.new
|
||||
user.name = "David"
|
||||
user.occupation = "Code Artist"
|
||||
user = User.new
|
||||
user.name = "David"
|
||||
user.occupation = "Code Artist"
|
||||
```
|
||||
|
||||
A call to `user.save` will commit the record to the database.
|
||||
|
@ -163,10 +163,10 @@ A call to `user.save` will commit the record to the database.
|
|||
Finally, if a block is provided, both `create` and `new` will yield the new object to that block for initialization:
|
||||
|
||||
```ruby
|
||||
user = User.new do |u|
|
||||
u.name = "David"
|
||||
u.occupation = "Code Artist"
|
||||
end
|
||||
user = User.new do |u|
|
||||
u.name = "David"
|
||||
u.occupation = "Code Artist"
|
||||
end
|
||||
```
|
||||
|
||||
### Read
|
||||
|
@ -174,23 +174,23 @@ Finally, if a block is provided, both `create` and `new` will yield the new obje
|
|||
Active Record provides a rich API for accessing data within a database. Below are a few examples of different data access methods provided by Active Record.
|
||||
|
||||
```ruby
|
||||
# return array with all records
|
||||
users = User.all
|
||||
# return array with all records
|
||||
users = User.all
|
||||
```
|
||||
|
||||
```ruby
|
||||
# return the first record
|
||||
user = User.first
|
||||
# return the first record
|
||||
user = User.first
|
||||
```
|
||||
|
||||
```ruby
|
||||
# return the first user named David
|
||||
david = User.find_by_name('David')
|
||||
# return the first user named David
|
||||
david = User.find_by_name('David')
|
||||
```
|
||||
|
||||
```ruby
|
||||
# find all users named David who are Code Artists and sort by created_at in reverse chronological order
|
||||
users = User.where(name: 'David', occupation: 'Code Artist').order('created_at DESC')
|
||||
# find all users named David who are Code Artists and sort by created_at in reverse chronological order
|
||||
users = User.where(name: 'David', occupation: 'Code Artist').order('created_at DESC')
|
||||
```
|
||||
|
||||
You can learn more about querying an Active Record model in the [Active Record Query Interface](active_record_querying.html) guide.
|
||||
|
@ -200,9 +200,9 @@ You can learn more about querying an Active Record model in the [Active Record Q
|
|||
Once an Active Record object has been retrieved, its attributes can be modified and it can be saved to the database.
|
||||
|
||||
```ruby
|
||||
user = User.find_by_name('David')
|
||||
user.name = 'Dave'
|
||||
user.save
|
||||
user = User.find_by_name('David')
|
||||
user.name = 'Dave'
|
||||
user.save
|
||||
```
|
||||
|
||||
### Delete
|
||||
|
@ -210,8 +210,8 @@ Once an Active Record object has been retrieved, its attributes can be modified
|
|||
Likewise, once retrieved an Active Record object can be destroyed which removes it from the database.
|
||||
|
||||
```ruby
|
||||
user = User.find_by_name('David')
|
||||
user.destroy
|
||||
user = User.find_by_name('David')
|
||||
user.destroy
|
||||
```
|
||||
|
||||
Validations
|
||||
|
|
|
@ -200,7 +200,7 @@ Halting Execution
|
|||
|
||||
As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks, and the database operation to be executed.
|
||||
|
||||
The whole callback chain is wrapped in a transaction. If any <em>before</em> callback method returns exactly `false` or raises an exception, the execution chain gets halted and a ROLLBACK is issued; <em>after</em> callbacks can only accomplish that by raising an exception.
|
||||
The whole callback chain is wrapped in a transaction. If any _before_ callback method returns exactly `false` or raises an exception, the execution chain gets halted and a ROLLBACK is issued; _after_ callbacks can only accomplish that by raising an exception.
|
||||
|
||||
WARNING. Raising an arbitrary exception may break code that expects `save` and its friends not to fail like that. The `ActiveRecord::Rollback` exception is thought precisely to tell Active Record a rollback is going on. That one is internally captured but not reraised.
|
||||
|
||||
|
|
|
@ -264,7 +264,7 @@ class Person < ActiveRecord::Base
|
|||
end
|
||||
```
|
||||
|
||||
The default error message for this helper is "_must be accepted_".
|
||||
The default error message for this helper is _"must be accepted"_.
|
||||
|
||||
It can receive an `:accept` option, which determines the value that will be
|
||||
considered acceptance. It defaults to "1" and can be easily changed.
|
||||
|
@ -293,7 +293,7 @@ This validation will work with all of the association types.
|
|||
CAUTION: Don't use `validates_associated` on both ends of your associations.
|
||||
They would call each other in an infinite loop.
|
||||
|
||||
The default error message for `validates_associated` is "_is invalid_". Note
|
||||
The default error message for `validates_associated` is _"is invalid"_. Note
|
||||
that each associated object will contain its own `errors` collection; errors do
|
||||
not bubble up to the calling model.
|
||||
|
||||
|
@ -328,7 +328,7 @@ class Person < ActiveRecord::Base
|
|||
end
|
||||
```
|
||||
|
||||
The default error message for this helper is "_doesn't match confirmation_".
|
||||
The default error message for this helper is _"doesn't match confirmation"_.
|
||||
|
||||
### `exclusion`
|
||||
|
||||
|
@ -348,7 +348,7 @@ alias called `:within` that you can use for the same purpose, if you'd like to.
|
|||
This example uses the `:message` option to show how you can include the
|
||||
attribute's value.
|
||||
|
||||
The default error message is "_is reserved_".
|
||||
The default error message is _"is reserved"_.
|
||||
|
||||
### `format`
|
||||
|
||||
|
@ -362,7 +362,7 @@ class Product < ActiveRecord::Base
|
|||
end
|
||||
```
|
||||
|
||||
The default error message is "_is invalid_".
|
||||
The default error message is _"is invalid"_.
|
||||
|
||||
### `inclusion`
|
||||
|
||||
|
@ -381,7 +381,7 @@ will be accepted. The `:in` option has an alias called `:within` that you can
|
|||
use for the same purpose, if you'd like to. The previous example uses the
|
||||
`:message` option to show how you can include the attribute's value.
|
||||
|
||||
The default error message for this helper is "_is not included in the list_".
|
||||
The default error message for this helper is _"is not included in the list"_.
|
||||
|
||||
### `length`
|
||||
|
||||
|
@ -471,24 +471,24 @@ Besides `:only_integer`, this helper also accepts the following options to add
|
|||
constraints to acceptable values:
|
||||
|
||||
* `:greater_than` - Specifies the value must be greater than the supplied
|
||||
value. The default error message for this option is "_must be greater than
|
||||
%{count}_".
|
||||
value. The default error message for this option is _"must be greater than
|
||||
%{count}"_.
|
||||
* `:greater_than_or_equal_to` - Specifies the value must be greater than or
|
||||
equal to the supplied value. The default error message for this option is
|
||||
"_must be greater than or equal to %{count}_".
|
||||
_"must be greater than or equal to %{count}"_.
|
||||
* `:equal_to` - Specifies the value must be equal to the supplied value. The
|
||||
default error message for this option is "_must be equal to %{count}_".
|
||||
default error message for this option is _"must be equal to %{count}"_.
|
||||
* `:less_than` - Specifies the value must be less than the supplied value. The
|
||||
default error message for this option is "_must be less than %{count}_".
|
||||
default error message for this option is _"must be less than %{count}"_.
|
||||
* `:less_than_or_equal_to` - Specifies the value must be less than or equal the
|
||||
supplied value. The default error message for this option is "_must be less
|
||||
than or equal to %{count}_".
|
||||
supplied value. The default error message for this option is _"must be less
|
||||
than or equal to %{count}"_.
|
||||
* `:odd` - Specifies the value must be an odd number if set to true. The
|
||||
default error message for this option is "_must be odd_".
|
||||
default error message for this option is _"must be odd"_.
|
||||
* `:even` - Specifies the value must be an even number if set to true. The
|
||||
default error message for this option is "_must be even_".
|
||||
default error message for this option is _"must be even"_.
|
||||
|
||||
The default error message is "_is not a number_".
|
||||
The default error message is _"is not a number"_.
|
||||
|
||||
### `presence`
|
||||
|
||||
|
@ -528,7 +528,7 @@ If you validate the presence of an object associated via a `has_one` or
|
|||
Since `false.blank?` is true, if you want to validate the presence of a boolean
|
||||
field you should use `validates :field_name, inclusion: { in: [true, false] }`.
|
||||
|
||||
The default error message is "_can't be empty_".
|
||||
The default error message is _"can't be empty"_.
|
||||
|
||||
### `uniqueness`
|
||||
|
||||
|
@ -570,7 +570,7 @@ end
|
|||
WARNING. Note that some databases are configured to perform case-insensitive
|
||||
searches anyway.
|
||||
|
||||
The default error message is "_has already been taken_".
|
||||
The default error message is _"has already been taken"_.
|
||||
|
||||
### `validates_with`
|
||||
|
||||
|
@ -714,7 +714,7 @@ class Person < ActiveRecord::Base
|
|||
validates :name, presence: { strict: true }
|
||||
end
|
||||
|
||||
Person.new.valid? #=> ActiveModel::StrictValidationFailed: Name can't be blank
|
||||
Person.new.valid? # => ActiveModel::StrictValidationFailed: Name can't be blank
|
||||
```
|
||||
|
||||
There is also an ability to pass custom exception to `:strict` option
|
||||
|
@ -724,7 +724,7 @@ class Person < ActiveRecord::Base
|
|||
validates :token, presence: true, uniqueness: true, strict: TokenGenerationException
|
||||
end
|
||||
|
||||
Person.new.valid? #=> TokenGenerationException: Token can't be blank
|
||||
Person.new.valid? # => TokenGenerationException: Token can't be blank
|
||||
```
|
||||
|
||||
Conditional Validation
|
||||
|
@ -917,7 +917,7 @@ validations fail.
|
|||
|
||||
Because every application handles this kind of thing differently, Rails does
|
||||
not include any view helpers to help you generate these messages directly.
|
||||
However, due to the rich number of methods Rails gives you to interact with
|
||||
However, due to the rich number of methods Rails gives you to interact with
|
||||
validations in general, it's fairly easy to build your own. In addition, when
|
||||
generating a scaffold, Rails will put some ERB into the `_form.html.erb` that
|
||||
it generates that displays the full list of errors on that model.
|
||||
|
|
Loading…
Reference in a new issue