Related differences

Ques 36. How does Validation works in Ruby on Rails?

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 %>

Is it helpful? Add Comment View Comments
 

Ques 37. How can you add custom validation on your model in Ruby on Rails?

Now custom validations takes it to the next step.

Say you want to confirm that the data meets certain criteria 

Is it helpful? Add Comment View Comments
 

Ques 38. What is Flash in Ruby on Rails?

Flash  is simply a way to pass some value to the next action. 

Anything you place in the flash will be exposed to the very next action and then cleared out.

Here’s an example:

def destroy

section = Section.find(params[:id])      
section.destroy      
FLASH[:NOTICE] = "SECTION DESTROYED."     
redirect_to(:action => 'list', :page_id => @page.id)    

end

Then wherever you want to use the flash you can write this code. I often put this snippet in the application.html.erb file, somewhere towards the top:

   <% if !flash[:notice].blank? %>         
<div class="notice">  
 <%= flash[:notice] %>

Is it helpful? Add Comment View Comments
 

Ques 39. HOW CAN YOU INSTALL THE MISSING GEMS THAT ARE REQUIRED BY YOUR APPLICATION IN THE SIMPLEST WAY in Ruby on Rails?

bundle install

Is it helpful? Add Comment View Comments
 

Ques 40. How can you implement internationalization in Ruby on Rails?

Ruby ships with i18n which is an internationalization gem. 

You need to create locale files and save them under the config/locales directory as:

en.yml

es.yml

fr.yml

The keys should match for each of these files. 

en:   main_page:     hello: “Hello”     welcome: “Welcome to   My Company” es:   main_page:     hello: “Hola”     welcome: “Bienvenido a Mi Empresa” fr:   main_page:     hello: “Salut”     welcome: “Bienvenue Ă  Mon Entrepriseâ€

ť In your code you would need to specify that the text would be locale specific. So change it to something like this:

.content   %h1     = t("main_page.hello")  
%p     = t("main_page.welcome")

Then you have to select the actual locale.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users: