Ruby On Rails Interview Questions and Answers
Ques 46. How can you implement Caching in Ruby on Rails?
Rails offers multiple ways to cache content.
Fragment caching is my favorite because it gives you the choice to fragment to pull a portion from the cache and the remaining from a real-time DB call.
Say you wanted to show all the orders placed on your website in real time and didn’t want to cache that part of the page, but did want to cache the part of the page which lists all products available, you could use this piece of code:
<% Order.find_recent.each do |o| %> <%= o.buyer.name %> bought <%= o.product.name %> <% end %> <% CACHE DO %> All available products: <% Product.all.each do |p| %> <%= link_to p.name, product_url(p) %> <% end %> <% end %>
Another technique that works well for static pages is page caching. This technique is often used for home pages and is super fast.
class ProductsController < ActionController CACHES_PAGE:index def index @products = Products.all end end
Ques 47. What is Filter and when is it called in Ruby on Rails?Ans.
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]
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.
Ques 48. What do Controllers task in Ruby on Rails?Ans.
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
Ques 49. What is RESTFUL routing in Ruby on Rails?Ans.
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:
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:
POST - /photos
GET - /photos/:id
GET - /photos/:id/edit
PUT - /photos/:id
DELETE - /photos/:id
Ques 50. How can you routes all routs of an application?Ans.
rake routes -- will display all routes for an application.
Most helpful rated by users: