Ruby On Rails Interview Questions Part-3

3.1 What is AJAX ?

Ajax is the common method by which websites can load new data without having to

reload the entire web page, enabling some impressive dynamic functionality and interaction

not previously available on websites. A prime example of Ajax is Google’s Gmail

service. When Gmail checks for new e-mail messages, it doesn’t refresh the web page.

Instead, it pings the Google servers and pushes out the new messages almost instantly.

 

3.2 What are Cookies?

Cookies, which are small files stored on your local computer, persist data when you are

using a website and want to store details for later use, such as personal site preferences

or the contents of your shopping cart.

 

3.3 What are RJS templates?

This addition allows you to generate Javascript from Ruby, which can be returned by Ajax calls and evaluated in the page. As usual, I use link_to_remote, Rails’ standard way to create an Ajaxified link:

link_to_remote "Add to Cart", :url => {
:action => 'add_to_cart', :id => product }

The controller saves the product id and renders the add_to_cart view—but instead of the usual .rhtml or .rxml template, it’s an .rjs template:

page.replace_html 'cartbox', :partial => 'cart'
page.replace_html 'num_items', :partial => 'num_items'
page.send :record, "Element.addClassName('product_#{@params[:id]}', 'incart')"

These three lines accomplish the same thing as the fourteen lines of Javascript above. The first line renders the ‘cart’ partial into the DOM element #cartbox. The second line does the same thing, but for the header. The third line just creates a line of Javascript to add a CSS class to an element.

3.4 How to use AJAX to load Rails partials ?

Click here http://emphaticsolutions.com/index.php/2008/04/05/using-ajax-to-load-rails-partials/