Tuesday, March 28th, 2006

Ruby on Rails 1.1 released – RJS Template, Easy XML Serialization, and More

Category: Books, Examples, Library, Prototype, Ruby

Ruby on Rails 1.1 has been released with an impressive set of new features for a point release. For a comprehensive list of new features, check out Scott Raymond’s post or the official annoucement.

The Ajax related features include:

  • RJS templates – Javascript generation in Ruby for common Ajax updates. Now in addition to .rhtml and .rxml, you’ll have .rjs files with ruby to generate your javascript. For example, taking from Scott’s example, the following pre-RJS JavaScript code:

    1. var Cart = {
    2.     add: function(product_id) {
    3.       Element.addClassName('product_' + product_id, 'incart')
    4.       new Ajax.Request('/account/add_to_cart/' + product_id,
    5.          { method: 'post', onComplete: Cart.refresh })
    6.     },
    7.     remove: function(product_id) {
    8.       Element.removeClassName('product_' + product_id, 'incart')
    9.       new Ajax.Request('/account/remove_from_cart/' + product_id,
    10.          { method: 'post', onComplete: Cart.refresh })
    11.     },
    12.     refresh: function() {
    13.       new Ajax.Updater('cartbox', '/products/cartbox')
    14.       new Ajax.Updater('num_items', '/products/num_items')
    15.     }
    16.   }

    Becomes this in Ruby using RJS:

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

    See the rdoc or Cody’s blog for more information and examples.

  • DRY controllers for Ajax clients, non-Ajax clients, and anyone else by responding to the HTTP Accept header. David goes into more detail as well as Jamis, but the basic idea is to let the client tell you what data format it wants back using the Accept header so that Rails can respond easily with one controller method. The following create method can handles requests from any browser that needs plain html, from a browser with JavaScript support (returns a RJS template by default), and from a web services call expecting XML (returns a HTTP 201 “created” code and nothing else).
    1. def create
    2.     @comment = Comment.create(params[:comment])
    4.     respond_to do |type|
    5.       type.html { redirect_to :action => "index" }
    6.       type.js
    7.       type.xml  do
    8.         headers["Location"] =
    9.           url_for(:action => "show", :id => @comment.id)
    10.         render(:nothing, :status => "201 Created")
    11.       end
    12.     end
  • Easy xml serialization with to_xml:
    1. @person.to_xml(:include => @company)


    1. <person>
    2.     <id>...</id>
    3.     ...
    4.     <company>
    5.       <id>...</id>
    6.       <name>...</name>
    7.       ...
    8.     </company>
    9.   </person>
  • End to end integration testing baked in
  • Updates Scriptaculous to 1.6 and Prototype to 1.5.

Whew. The core team is definitely not resting on their laurels. With 1.1 out and over fifteen Ruby or Rails books coming out this year, Ruby on Rails does not appear to be losing any momentum.

Posted by Rob Sanheim at 11:52 am

3.7 rating from 59 votes


Comments feed TrackBack URI

[…] here. […]

Pingback by AlbanyWiFi.com » Blog Archive » Ruby on Rails 1.1 released — March 28, 2006

[…] Ajaxian » Ruby on Rails 1.1 released – RJS Template, Easy XML Serialization, and More (tags: rjs rubyonrails javascript) […]

Pingback by just in ram » links for 2006-03-29 — March 28, 2006

In fact, since I wrote the RJS example you cited, the syntax has dramatically improved, so the Ruby code example is now far more readable:

  page[‘cartbox’].replace :partial => ‘cart’
  page[‘num_items’].replace :partial => ‘num_items’
  page[“product_#{params[:id]}”].addClassName ‘incart’

Comment by Scott Raymond — March 28, 2006


Comment by rt — April 16, 2006

How about undesiered javascript code poping on the screen along with the do desiered effect.

if I ask in the rjs for it to highlight some element, it does it allright, but it also prints to screen some javascrip output, something like:

new Effect.Highlight(“sitio13”,{duration:3}); } catch (e) { alert(‘RJS error:\n\n’ + e.toString());

Any help would be great, not sure if im right asking for it here though.

Comment by leo — May 16, 2006

Leo, maybe you specify the :update option to update a certain div which you don’t want?

Comment by piggy — May 20, 2006


Comment by sdf — June 29, 2006


Comment by aa — January 20, 2007

Leave a comment

You must be logged in to post a comment.