Hi, my name is Nicholas Schlueter [schlueter [at] gmail com], I blog at simpltry about javascript (mostly).

Dependencies

Installation

script/plugin install http://svn.simpltry.com/plugins/active_jax
			

Short Tutorial:

ActiveJax is a RoR plugin. It’s goal is to be an effective ActiveRecord -> Prototypejs bridge, allowing you to directly call your finders from javascript. Alright let’s see some code:

Ruby Model:

class Author < ActiveRecord::Base
        active_jax

        def find_by_name(n)
          find(:all, :conditions => {:name => n})
        end
      end
      

Java Script:

ActiveJax.Author.find_by_name("Nicholas Schlueter").
        each(function(author) {
          alert(author.name);
        });

      

As you can see you just call the finder by name in the javascript.

It can also do belongs_to associations:

Ruby Model:

class Author < ActiveRecord::Base
        active_jax :include => :publisher

        belongs_to :publisher

        def find_by_name(n)
          find(:all, :conditions => {:name => n})
        end
      end

      

Java Script:

ActiveJax.Author.find_by_name("Nicholas Schlueter").
        each(function(author) {
          alert(author.publisher.name);
        });
      

Hold on, isn’t that pretty dangerous. It’s true, exposing your model can be dangerous. There is some security built in. Building on our previous example:

Ruby Model:

class Author < ActiveRecord::Base
        active_jax :include => :publisher, :excluded_columns => :email

        belongs_to :publisher

        def find_by_name(n)
          find(:all, :conditions => {:name => n})
        end
      end

      

Java Script:

ActiveJax.Author.find_by_name("Nicholas Schlueter").
        each(function(author) {
          alert(author.publisher.name);
        });
      

Hurray! The email address is no longer published to all would be spammers (bastards).

In order for all this to work you have to do 1 more thing, include the following somewhere in your html doc:

<%= active_jax_include %>
      

There is also a way to enable controller actions, but to be honest it is only partially thought through.

Sample App:

ActiveJax Sample Application