During my daily reading of the Ruby on Rails mailing list I ran across a post asking about RJS templates. I hadn't heard of them before and I was intrigued to find out more.
It turns out that RJS templates are a new form of template, called JavascriptGeneratorTemplates, and end with a .rjs file extension.
Ok, so there is a new type of template, but what does it do? Here is the description from the rdoc documentation in the changeset:
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.
Trying out the rjs templates requires checking out the latest Rails code from the subversion repository. You can use these instructions to help get your project running on edge Rails. After you are running edge Rails be sure to run the following:
rake update_javascripts
First, create the view that will be updated by the rjs template:
1 2 3 4 5 6 7 8 9 |
<h1 id='header'>RJS Template Test</h1>
<ul id='list'>
<li>Dog</li>
<li>Cat</li>
<li>Mouse</li>
</ul>
<%= link_to_remote("Add a fox",
:url =>{ :action => :add }) %>
|
Ensure that your layout is including the javascript libraries:
1 2 |
<%= javascript_include_tag :defaults %> |
Then add the action that will be called by link_to_remote to your controller:
1 2 3 |
def add end |
Rails now looks for templates with an extension of .rjs, in addition to .rhtml and .rxml, so create a view named add.rjs for your controller and add the following code to it:
1 2 3 4 5 6 |
page.insert_html :bottom, 'list', content_tag("li", "Fox") page.visual_effect :highlight, 'list', :duration => 3 page.replace_html 'header', 'RJS Template Test Complete!' |
If you're curious where the page object comes from, the rdocs provide the explanation:
An instance of the JavaScriptGenerator object named +page+ is automatically made available to your template, which is implicitly wrapped in an ActionView::Helpers::PrototypeHelper#update_page block.
Now when you load the page in your browser and click the 'Add a fox' link, a Fox list item will be added to the list and the entire list will be highlighted. The header will also be updated. You can also render partials:
1 2 |
page.replace_html 'header', :partial => 'my_partial' |
As you can see from the example, the new RJS templates make it dead simple to update multiple page elements using AJAX. This example barely scratched the surface of what is possible. Kudos to the Rails developers for yet another wonderful addition to the framework.
Checkout the new RJS Element and Collection Proxies.
No longer do you need Edge Rails or any plugin to get RJS working. Just upgrade to Rails 1.1 to get RJS baked right into the framework. Have fun!
Sorry, comments are closed for this article.
November 28th, 2005 at 11:20 PM Nice job Cody! Thanks for writing all this up. I'll be posting an RJS video on the Rails blog soon.
November 29th, 2005 at 01:53 AM Wow, that makes the code soooo much cleaner!
November 29th, 2005 at 03:41 PM Yeah, that's fantastic.
November 30th, 2005 at 04:34 AM I'm looking forward to the video!
November 30th, 2005 at 06:04 AM is it possible to use actual javascript inside a .rjs template..
November 30th, 2005 at 01:51 PM This looks great but will it make it into the 1.0 release?
November 30th, 2005 at 08:43 PM Ryan, check out this [code](http://techno-weenie.net/svn/projects/rails_help/app/views/questions/create.rjs) from [Rick Olson](http://techno-weenie.net/). It shows a hackish way of sending JavaScript to the page.
November 30th, 2005 at 08:54 PM Stuart, it looks like RJS templates will not be included in the 1.0 release of Rails. [Marcel Molina Jr.](http://vernix.org/marcel/) just posted the following to the Rails mailing list regarding the inclusion of RJS templates in 1.0: >It will not be. We've talked about making it available as a plugin though for those who want it but must stay back on 1.0.
January 3rd, 2007 at 11:56 PM rake update_javascripts has been deprecated, you can now use: rake rails:update:javascripts