Saturday, November 15, 2008

Merb Javascript Delete link_to with Prototype.js

I got on the elevator to go home from work the other day and this other guy in the elevator says to me "Those look like comfortable shoes." Huh? Did I just step in the world of Forest Gump? What is that all about?

Currently there is quite a pissing match going on in the world of Rails with fronts against Zed Shaw and Merb - see here and here. I hope all this energy can be channeled to improving frameworks and not just spewing internet bile.

Anyway, did a little playing around with Merb - just wanted to see what it would take to set up a resource, a restful controller, and how it would compare to Rails. I came away impressed, Merb appears to be another great Ruby web framework. Right now it would appear the biggest deficiency in the project is documentation. Merb is currently woefully lacking in documentation compared to Rails.

Among a bunch of small difference the url helpers are on of the biggest differences. The Merb way is demonstrated here. The last thing I needed to figure out was how to get Merb to generate a delete link that uses javascript to post back to the controller - you know something like this in Rails:



After looking a Merb's link_to documentation for a while I realized - "ah, Merb is Javascript library agnostic and also wants to be unobtrusive." So, instead of looking for someone else's canned solution I cooked up my own using Prototype. Just add a class to your link:



and then hook in your events with some prototype:



The url(:delete_xxx, @xxx) in Merb actually creates a link to /xxx/:id/delete where you can place an "Are you sure?" form for users with Javascript turned off. The above code will bypass that if Javascript is enabled.

Ok, enough playing around with Merb for now, time to look at Sinatra - only 1576 LOC!