Originally published at Aerialistics. You can comment here or there.
Lately, I’ve been toying with the idea of Ruby on Rails. Lots of good things have been said about Ruby on Rails, and as I’ve been discovering it lately, I realize that, like any overly-hyped thing, it doesn’t quite live up to it’s hype. At least not yet.
I’m still learning lots about Rails and Ruby. Both are fascinating in their own right, and I’m going to at least make a website or two with Rails, I’m not sure how much it will replace django. Development does seem to go faster than django, so maybe I will. Who knows.
One very big thing that i like is the focus on RESTful development. REST is what the web really was designed for, back in the day even if the term REST wasn’t mentioned for years to come.
I like RESTful development, so this is one thing that I really like.
One of the big things I like about REST is the concept of “format”. Information about a resource can be returned in multiple formats. Rails natively supports that with its “respond_to” function.
So right now, I’m working on a website that will display Donations by the charity that they were donated to. Basically, if I have Charity X, and I’ve donated $1000 to it, and I have Charity Y, and I’ve donated $500 to it, I want to represent that data.
So naturally, for a list of donations by charity, my rest url should be this: /donations . Now, the default rendering of this will be an html table that lists each charity and the total amount donated to them. The Charity links to the Charity page, and the donation links to details about the individual donations made.
However, I want to display a bar chart that shows the level of donations made for each charity.
So, keeping with REST, my url should be /donations.png right? To show that it’s a graphic representing the resource list of donations.
Fabulous! And it works out of the box using the respond_to function. However, here’s the rub. Since I’m using the Rails map.resource to setup my RESTful API, I’m using:
<%= image_tag url_for(donation_path(:format=>"png")) %>
Now, does this generate my perfectly restful url above (/donations.png)? Nope. It sure doesn’t. Instead, the above generates /donations?format=png, which is not right. Does it work? Sure thing because of a really neat feature in rails.
That feature is that it looks at the Accepts header of the request to determine the format (if there is no file extension). Since Firefox (haven’t tried IE) sends Accepts for image/png when loading an image tag, it works, but the URL isn’t correct.
I’m still trying to find how to fix the URL issue so that it’s right, and I’ll post a follow-up once I know the answer.
All in all, rails is neat.