Ruby%20On%20Rails%20Interview%20Questions%20and%20Answers
Question: How does Validation works in Ruby on Rails?Answer:Validation means checking to see if data is good before it is stored in the database. During signups and other such user input cases you want to check and be sure that the data is validated. In the past developers would often put this type of validation logic as triggers in the database. In an MVC architecture one can do validations at each level. You can do validations in the controllers but it is usually a good idea to keep your controllers skinny. Views suffer from the javascript limitation because javascript can be disabled on the client side so they are not completely reliable. The best way to manage validation is to put it in the model code. This model code is really the closest as you can be to the database and works very well for Rails applications. Here are a few validation examples: class Person < ActiveRecord::Base validates :name, :length => { :minimum => 2 } validates :points, :numericality => { :only_integer => true } # only integer validates :age, :numericality => { :greater_than => 18 } # greater than 18 validates :email, :uniqueness => true validates :email, :confirmation => true # this is to validate that the two email fields are identical validates :email_confirmation, :presence => true # this is to validate that the email confirmation field is not nil In your view template you may use something like this: <%= text_field :person, :email %> <%= text_field :person, :email_confirmation %> |
Most helpful rated by users:
- What is Ruby On Rails?
- Why Ruby on Rails?
- Explain how (almost) everything is an object in Ruby.
- What are Gems and which are some of your favorites?
- How would you declare and use a constructor in Ruby?