About Rails Validations

Kevin gotealeaf homework done today, there is a topic about validations of:

* How exactly do Rails validations get triggered? Where are the errors saved? Why we need the ‘render’ to show the validation messages on the user interface? *


Here is my own understanding:

Suppose we have a Category class variables are name, set in the model file validates.

2.0.0-p247 :019 >   Category
 => Category(id: integer, name: string, created_at: datetime, updated_at: datetime) 

model/category.rb

class Category < ActiveRecord::Base
  validates      :name, presence: true
end 

* 1. How exactly do Rails validations get triggered? *

Check the documentation for Rails,ActiveRecord::Base include:

  • ActiveRecord::AutosaveAssociation

  • ActiveRecord::Validations.

ActiveRecord::AutosaveAssociation says:

If validations for any of the associations fail, their error messages will be applied to the parent.

So, if no name assignment, when we saving the instance variables. ActiveRecord::AutosaveAssociation find that validations fail,applied the error messages.

* 2. Where are the errors saved? *

2.0.0-p247 :023 >   category.errors
 => #<ActiveModel::Errors:0x007fec3dbda728 @base=#<Category id: nil, name: "zoe", created_at: nil, updated_at: nil>, @messages={}> 

Found errors in @messages are stored inside.

Here is a look at how the content of messages generated.

Referring to the document, the customized model validates. Requirements name beginning with the first letter z.

class Category < ActiveRecord::Base 
validates_each :name do |record, attr, value|
  record.errors.add attr, 'starts with z.' if value.to_s[0] == 'z'
end 
end

check in console:

 => true
2.0.0-p247 :010 > category = Category.new
 => #<Category id: nil, name: nil, created_at: nil, updated_at: nil>
2.0.0-p247 :011 > category.valid?
 => true
2.0.0-p247 :012 > category.errors.messages
 => {}
2.0.0-p247 :013 > category.name = 'zoe'
 => "zoe"
2.0.0-p247 :014 > category.valid?
 => false
2.0.0-p247 :015 > category.errors.messages
 => {:name=>["starts with z."]}
2.0.0-p247 :016 > 

errors messages successfully modified.I guess validates :name, presence: true produce the default messages:name:["can't be blank”].We can write validations methods themselves.

* 3. Why we need the ‘render’ to show the validation messages on the user interface? *

Searching for 'rails controller lifetime':

When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the method with the same name as the action.

When the next page request, controller of the life cycle. @ messages is an instance variable @ controller, and will be destroyed along with @ controller and destroyed. So, to use render to hold live page, so the page does not refresh.

* The content is not clear: *

  1. If validations for any of the associations fail, their error messages will be applied to the parent.

Here's parent refers errors or @ messages it?

  1. How to call the following piece of code?

    <ActiveModel::Errors:0x007fec3dbda728 @base=#<Category id: nil, name: "zoe", created_at: nil, updated_at: nil>, @messages={}> 
    

Neil Wang

产品经理。喜欢摄影,滑板,电影。微信公众号:NeilOnly