Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

KRJS – Extending Rails views with automatic element AJAX events

By Peter Cooper / June 14, 2006

KRJS is an extension to Rails views by Chew Choon Keat that helps provide a radically different mechanism for handling AJAX and RJS. He calls it "RJS without messing the views."

Rather than use remote_form_for and link_to_remote, for example, you can create specifically named actions on your controller that use RJS to perform the relevant actions upon regular code, rather than clog your views up with JavaScript and AJAX snippets. For example:

<%= form_tag({:action => 'submit'}, {:id => 'form'}) %>
  <%= text_field 'account', 'login', :id => 'account-new-login' %>
  <%= submit_tag 'Login' %>
<%= end_form_tag %>

This code looks like a normal form, but with KRJS, when the form is submitted the on_form_submit action is called on the controller via AJAX. When the account_login text field is blurred, on_account_login_blur on the controller is called via AJAX.

It's a bit heavy to fully explain here (there's a great tutorial already), but it's an interesting development and could eventually become a crucial part of AJAX development with Rails. The paradigm is powerful (Did I just say that?).


  1. Danger says:

    If this is what I think it is, this could definitely become the new standard. I'll have to check out that tutorial.

Other Posts to Enjoy

Twitter Mentions