RJS Templates

Posted by greg Mon, 06 Feb 2006 19:46:00 GMT

We had a brief introduction session about RJS templates (a way to automate AJAX actions for Rails) recently. The HOWTO document appears below. We've also provided an mp3 download of the audio from the presentation we had for the team.

About

RJS Templates are similar to .rhtml templates except they contain Javascript commands and have an .rjs file extension. The action/view base naming scheme remains the same. Unlike conventional templates which are used to render the results of an action, these templates generate instructions on how to modify an already rendered page. This makes it easy to modify multiple elements on your page in one declarative Ajax response. Actions with these templates are called in the background with Ajax and make updates to the page where the request originated from.

Currently supported RJS methods/features:

  • insert_html
  • replace_html
  • show
  • hide
  • visual_effect
  • alert
  • redirect_to
  • call
  • assign
  • <<
  • toggle
  • delay

Install

There are currently two ways to enable RJS templates in your rails app. The simplest way is to update your rails app to use edge rails. The second, slightly more involved way is to install RJS templates as a plugin.

Edge Rails

Edge Rails is a term that means you are running a local copy of the latest SVN trunk of Rails, instead of using gems. By checking out the trunk into your /vendor dir, Rails will automatically use that instead of the gems installed on your machine.

The easiest way to get your rails app running on edge rails is to unpack the latest edge rails into your application vendor directory using the following command:

rake freeze_edge
rake update_javascripts

This puts the entire rails framework into your application. This also allows you to run on computers without rails installed.

To undo it:

rake unfreeze_rails

More info on Edge Rails can be found here: http://wiki.rubyonrails.com/rails/pages/EdgeRails

Plugin

To install RJS Templates as a plugin, do the following:

script/plugin discover

This will question you whether or not you want to add the plugin repositories that have been listed on the Rails Plugins wiki page to your local list of plugin repositories. Ensure that you add the codyfauser.com repository when prompted. You can ensure that you have the correct repositories by executing:

script/plugin sources

Now do:

script/plugin install javascript_generator_templates

Alternately if you don't want to add the codyfauser.com repository to your list of plugin sources you can install the plugin in one shot as follows:

script/plugin install \
http://www.codyfauser.com/svn/projects/plugins/javascript_generator_templates/

Example

Categories List

SQL:

CREATE TABLE categories ( id serial NOT NULL, name character varying(64) );

Initial Setup:

rails rjs_demo cd rjs_demo rake freeze_edge rake update_javascripts script/generate controller home index add script/generate model category

View Template app/views/home/index.rhtml:

<%= form_remote_tag :url => { :action => 'add' } %> Category: <%= text_field 'category', 'name' %> <%= submit_tag 'Add' %> <%= end_form_tag %> <ul id="categories"> <% @categories.each do |category| -%> <li><%= category.name %></li> <% end -%> </ul>

Nothing special, just a category list and a form to add more categories. Note the <ul> tag is identified as 'categories'. This would work the same if it were a <table> or <tbody>.

Controller Methods app/controllers/home_controller.rb:

class HomeController < ApplicationController def index @categories = Category.find( :all ) end def add @category = Category.new if request.post? @category = Category.create( params[:category] ) end end end

Posting adds the category to the database and assigns it to @category which will be used in the RJS template. After the action completes it looks for a template named add.*, and ours is called add.rjs so it uses that.

RJS Template app/views/home/add.rjs:

if @category && @category.errors.empty? li = content_tag( 'li', @category.name ) page.insert_html :bottom, 'categories', li page.visual_effect :highlight, 'categories', :duration => 3 end

If the category was created with no errors then we add it to the bottom of the 'categories' list. The magic here is the 'page' object. With the page object you can work on most any element in the html document. All the methods listed at the top are available.

Comments

  1. John said 55 days later:

    You forgot to include:

    <%= javascript_include_tag :defaults %>

    in the app/views/layouts/home.rhtml file (and the home.rhtml layout file!)

  2. Sreenu Vemu said 73 days later:

    Hi,

    I am Sreenu Vemu from Germany. I am very much interested in Ruby on Rails and would like to know about the following:

    1) Can RoR be used to develop webframe works like eBay? I mean can RoR handle the complexity involved in developing and maintaining a platform like eBay?

    You answer can help me a lot…

    cnu.vemu@gmail.com

    Thank you and looking forward to your mail..

    Regards, Sreenu

  3. Kebab King from Jackson Heights said 89 days later:

    Greetings, Sreenu!!!

    I am, too, very much having an interest in these rails happenings and the ebay! I am quite politely in the development of many several of “ebay frameworks” (but not ebay hahaha) and the rails are “work very sufficiently” for my needs. I think you are doing the Right Thing and even though you are “very stupid” right now, with hope and much Prayer to teh god you can develop one very similar ebay framework as I do. I wish you best lucks and may the fat loins of your wife bear many “strong boy”!!

  4. Jabberwock said 94 days later:

    We are currently working on what (to my knowledge) will be the largest site written in Ruby on Rails. It is www.talentines.com. Right now it is a splash page, but will eventually feature an online community targetting talent (programmers, actors, poets, musicians, painters, models, etc). It will feature online auctions, streaming media, profiles, insite messaging, voting polls, celebrity profiles and TONS of other stuff. So to answer your question – Yes. RoR can be used for a site on the magnitude of eBay.

  5. zyx (zyx_boy@yahoo.com) said 130 days later:

    Followed your steps. I can view:

    <http://localhost:3000/home/index> fine

    When I add a category, it gets added to the database fine. But the action then goes to: <http://localhost:3000/home/add>

    add.rhtml:

    Home#add

    Find me in app/views/home/add.rhtml

    I thought this action would go to add.rjs?? Any ideas as to where I am going wrong?

    Am using: ruby 1.8.4 (2005-12-24) [i686-linux] Rails 1.1.2

    and i did do: rails rjs_demo cd rjs_demo rake freeze_edge rake update_javascripts script/generate controller home index add script/generate model category

    Any ideas as to where I am going wrong?

Comments are disabled