Observing Fields With Rails in the Browser

Posted by Rick DeNatale Sun, 07 Oct 2007 16:34:00 GMT

The AJAX support in Rails is great, but sometimes you don't want to have to make an unnecessary trip to the server to make your UI dynamic. For example, let's say you want to do something like Basecamp does when you create a new message. If you have a Basecamp setup to allow you to talk to both members of your team and also clients, it provides some nice checkboxes to let you control who will get notified of the new message. Those checkboxes are grouped with a checkbox for each company, and then checkboxes for all of the people from that company below the company checkbox. If you check or uncheck a company checkbox all of the people listed under the company are checked or unchecked to match.

This can all be done within the browser. It just requires manipulation of existing elements within the domain object model, so there's no need to go back to the server.

Rails provides the observe_field helper method which is normally used to generate a remote request when a DOM element changes. It can be used to do what we want, but how to do so isn't well documented, either in the Rails documentation, or via google.

So here's how to do it.

Read more...

Posted in  | Tags ,  | 1 comment | no trackbacks