Posts from June 2008
Organize Nested Resource Controllers More Efficiently in Rails
Nested resources are awesome. If you want to have a blog with entries that have comments, the resources are dead simple:
map.resources :entries do |entry|
entry.resources :comments
end
Awesome, now you have set up paths like entries/3/ and entries/3/comments/ using an EntriesController and a CommentsController. What if, however, you want to have a base comments resource (/comments/) to show all comments? You already have a CommentsController, and it doesn’t do what you want. To solve this, I do something like this:
map.resources :entries do |entry|
entry.resources :comments, :controller => 'entries/comments'
end
map.resources :comments
Now, you have all your routes set up, and you can have separate controllers, Entries::CommentsController (which lives in controllers/entries/comments_controller.rb) and CommentsController. Just remember when generating the nested controller to use the proper name, like so:
ruby script/generate controller entries/comments
Firefox 3 and Firebug
Problem: Firefox 3 (which was released today) does not work with Firebug. Firebug’s website is down.
Solution: I found a developer build of Firebug here that seems to work pretty well. Hopefully the actual developers of Firebug can get their site up soon, but in the meantime, this seems to be working pretty well.
So far, I’m liking Firefox 3, and in the next couple of days, I’ll be posting an entry with my observations and thoughts.
Follow Euro 2008 on Twitter
After my friend Paul asked me to text him updates from a Euro 2008 game while he was at work, I came up with the idea to set up a twitter bot that posted updates from Soccernet’s gamecasts. After a couple hours of work, using Hpricot and the Twitter gem, I was able to build a Ruby script that worked pretty well. I’m planning on posting a complete post on how I did it, but first I want to work some of the kinks out. There are three twitter accounts you can follow if you’re interested:
- @euro2008cup - All updates from the gamecast (about one post per minute in the game). Tweets are shortened with a link to the full update.
- @euro2008scores - Only updates from the gamecast that are marked as “alerts” (goals, for the most part). Like @euro2008cup, updates are shortened.
- @euro2008sms - This has the same updates as @euro2008scores, but updates longer than 140 characters span across multiple tweets. This is best if you want to follow on your phone, as you’ll get the full update instead of having to click on a link.
Hopefully these will help someone out. Like, I said, I’ll be posting a how to on making your own twitter bot and screen scraper, so keep an eye out for that.
The Mysterious Save For Web Color Shift
Finally, the mysterious “Save For Web” color shift in Photoshop is solved. Awesome!
Advanced Rails Recipes
A couple weeks ago, I received my copy of Advanced Rails Recipes by Mike Clark (and others), and so far, I’ve really been enjoying it. The first book by Chad Fowler was easily my favorite Rails book at the time, and the new edition has definitely continued the trend. I’ve always learned best from examples, and the recipes in the book are all top-notch, real world scenarios that can be quickly and easily applied to your application. Already, I’ve used many of them while working on my site, and below are a few of my favorites.
- Respond To Custom Formats
- Freshen Up Your Models with Scope
- Handle Multiple Models in One Form
- Upload Images with Thumbnails
- Send E-mail via Gmail
- Preserve Files Between Deployments
- Give Users Their Own Subdomain
I definitely recommend the book to any Rails developer, new or old, because the book truly has something for everyone.

