Using Mootools with Ruby on Rails
This one was built and tested, using Rails 2.3.2 and Mootools 1.2.2
I’ve been playing around with Rails for about two months now and I LOVE it, because they integrate so many best practices for a web developer.
The only thing I didn’t like was the integration of Javascript.
I don’t wanna use Prototype/Scriptacolous
Both is already built in. I’m not saying these are bad libraries, but I prefer Mootools for my clientside scripting.
Rail’s Ajax Helpers ain’t good JS scripting
I don’t see any sense in writing javascript code in rails. I want my Javascript unobtrusive and nicely degrading and want to take all benefits of my js framework. At least at this point, I think it’s better to write js in a js environment (but mind that I’m just a rails beginner, maybe there are better ways).
A Mootools Factory Pattern for Ajax Requests
Due it’s raining this sunday, I’ve put a little class together that makes it much more easier to create Ajax requests against your RESTful Rails ressources, using Mootools.
Let’s assume, you have a controller for a ressource Customers and you want to call the default scaffolding methods of Rails using Ajax, which would be index, show, new, create and destroy.
Prepare your Controller
First of all, you have to add a response format to the methods in your controller. We’re lucky, because Rails supports the js format and rendering as JSON format by default (pretty much the same like the xml format). So you just have to add a few lines of code for every method, like this:
format.js { render :json => @customer }
(There is a whole example controller in the download)
Get the Rails Auth Token in JS
As a additional security mechanism, Rails added a Auth Token. Matt from madhatted showed us how to get the auth token into Javascript without a need of a form. This is what you also need to do, to use my code.
So insert this into your view, where you load your Javascript:
<%= javascript_tag "var RAILS_AUTH_TOKEN = #{form_authenticity_token.inspect};" if protect_against_forgery? %>
Of course, you should also add Mootools at this point and my lib.
Use it!
Now you can use my lib like this:
New instance for customer controller
customer = new RailsRequest({controller: "customer"});
Get json formatted string of all customers
customer.index().send();
Get json formatted string of customer with id 2
customer.show(2).send();
Create a new customer
customer.create({name: "Elvis", is_cool: true}).send();
Update customer with id 2
customer.update(2, {is_cool: false}).send();
Delete customer with id 7
customer.destroy(7).send();
You can add default callbacks for all kinds of methods. Just have a look at my code.
Important Notes
I’m pretty sure this is far from perfect, either by the rails and the mootools perspective. It was just a tryout, to proof myself that both frameworks can play well together.
If you know a better solution or you’re happy with this one, please let me know in the comments.
Download
Here it is: mootools_with_rails Have fun!
Edit: 2009-05-04 Some minor fixes.










4 Comments, Comment or Ping
tom
good, aber what about form.send() ajax?
Mai 21st, 2009
chris
Good question! This was extracted from a project to support inline editing, but maybe I’ll add form.send later.
Mai 21st, 2009
oelmekki
Nice class, thanks.
IMO, the main degradability problem in rails still remains the http verb case.
Did you find a way to do some link_to something, :method => :delete or :put without inline prototype code?
(or more generally : did you find a way to work with resources with no obstrusive js?)
Okt 12th, 2009
chris
You have to work with Forms and a _method field.
The rails link_to [...] :method => :delete does the same. It creates a form using JS (not prototype) and submits it.
Of course you can create your own forms by default and replace them with mootools, to use this class instead.
Okt 12th, 2009
Reply to “Using Mootools with Ruby on Rails”