Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Ruby On Rails Interview Questions and Answers

Ques 26. What is Filter and when is it called in Ruby on Rails?

Filters are methods that are called either before/after a controller action is called. 

Say a user requests a controller action such as userdashboard/index

In such a case a filter can be setup so that the UserDashboard/index page is only accessible to loggedin users by adding the following lines towards the beginning of the page:

class UserDashboardController < ApplicationController

  before_filter :confirm_logged_in,  :except => [:login, :attempt_login, :logout]  
def index
....
end

def login
....
end

def attempt_login
....
end

def logout
....
end

end  

In the code above the condition “confirm_logged_in” is checked before all actions, except login, logout & attempt_login. 

After filters (after_filter) are not used too much but they have the effect of executing some code after a particular action has completed. 

Think of them like triggers that get executed automatically — just like a database trigger. 

Is it helpful? Add Comment View Comments
 

Ques 27. What do Controllers task in Ruby on Rails?

Once a request comes into the Rails stack, it goes to the routes table to determine which controller and action should be called. 

Once a controller action is determined the request is routed to the controller and it does the needed processing by connecting with the DB if needed and then it sends control to the View to render the output. 

So, really the flow for Rails goes somewhat like this:

Customer-> Routes-> Controller -> Model(DB) -> Controller -> View -> Customer

Is it helpful? Add Comment View Comments
 

Ques 28. What is RESTFUL routing in Ruby on Rails?

Routing is fun. If you have ever dealt with IIS you will fall in love with RESTful routing. Here’s how it works. 

Say you want your users to have access to certain pages such as:

/photos/new

/photos/1/edit

/photos/1

And, you want the right controller to get called.

And, you want the right view to get rendered.

All this is made possible with a single entry in the routes.rb file.

In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database. The single entry in the routing file creates seven different routes in your application, all mapping to the Photos controller:

GET-/photos

GET-/photos/new

POST - /photos

GET -  /photos/:id

GET -  /photos/:id/edit

PUT - /photos/:id

DELETE - /photos/:id

Is it helpful? Add Comment View Comments
 

Ques 29. How can you routes all routs of an application?

rake routes -- will display all routes for an application.

Is it helpful? Add Comment View Comments
 

Ques 30. How can you send a MULTI-PART Email?

Nowadays most email clients support HTML email, however there are still some old Blackberry phones that prefer emails the ‘ol text way. 

Therefore it is important to send emails both as HTML and text. This technique is called multi-part emails. 

The ActionMailer class (included in Rails 3.0) does a great job of sending both text and HTML emails out to the end user at the same time. 

By default Rails sending an email with plain/text content_type, for example:

# app/models/notifier.rb
def send_email(email)
  subject       email.subject
  from          email.from
  recipients    email.recipients
  sent_on       Time.now
  body          :email => email
end

Next let’s update the view in : app/views/notifier/send_email.html.erb

Welcome to here: 

The sent email is a plain text email

Date: Thu, 5 Aug 2010 16:38:07 +0800
From: RailsBP 
To: flyerhzm@gmail.com
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8

Welcome: http://rails-bestpractices.com

The link url is just displayed as a plain text because of the email content_type.

TEXT/HTML

If we want the email clients to display link url as html format, we should change the content_type to text/html in the app/models/notifier.rb file

def send_email(email)
  subject          email.subject
  from             email.from
  recipients       email.recipients
  sent_on          Time.now
  content_type     "text/html"
  body             :email => email
end

Now the sent email is a html formatted email

Date: Thu, 5 Aug 2010 17:32:27 +0800
From: RailsBP 
To: flyerhzm@gmail.com
Mime-Version: 1.0
Content-Type: text/html; charset=utf-8

Welcome: http://rails-bestpractices.com

Now the email client can display the link url correctly with html format.

The email header looks somewhat like this:

Content-Type: multipart/alternative;
boundary="----=_NextPart_000_002C_01BFABBF.4A7D6BA0"
Content-Type: multipart/alternative tells the e-mail program to expect different parts to follow, separated by a boundary which specified in quotation marks. Actually the boundary could be anything, though hyphens, equal signs, and underscores insure that the e-mail program won't try to display this boundary to the recipient.
------=_NextPart_000_002C_01BFABBF.4A7D6BA0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook