Rails, Firefox, Anime, Mac
A few days ago I noticed that some of the pages on the Hotels app on wego.com were completely unstyled. They turned out looking rather Jakob Nielsen-istic:

But we were attached to our ugly shade of green to leave those pages in their naked glory. Preliminary CSI work told me that some cached stylesheets generated by Rails were empty files. Why is this is happening?
Was I overriding the stylesheets generated by Rails in different pages? Because we have a lot of cobranded sites and country sites on wego.com, I use the :cache option when using stylesheet_link_tag very often.
For example, the main wego.com site’s layout template has a stylesheet_link_tag like this (in reality there are a whole lot more stylesheets):
<%= stylesheet_link_tag 'yui/reset-fonts', 'search', :cache => 'cache/search/listings' %>
When I need to make a new page for a cobranded site, I’ll create a new layout template with this:
<%= stylesheet_link_tag 'yui/reset-fonts', 'search', "sites/#{current_site}/cobrand", :cache => "cache/#{current_site}/search/listings" %>
Oftentimes I’d copy and paste (boo and hiss all you want!) the stylesheet_link_tag from one layout template to another and forget to update the cache path (the :cache => '/path/to/stylesheet' part). Two different stylesheet sets being cached to the same path is naturally a very stupid thing to do. So this wasn’t it, but it’s good to point this out because I have made this mistake at least 2 times!
Next, since I was using Sass, I was by now pretty sure that was it. First things first: did I check in a generated CSS file into source control (we use Git)? It’s another amateur mistake, but unsurprisingly, I’ve done this a couple of times. I think I’d wasted about an hour hunting down the reason for a style change that just wouldn’t show up. Yeah, I could have just added *.css to .gitignore, but I’m still using a mix of pure CSS and Sass templates.
In the end, I found this blog post by Ari Lerner on the CitrusByte blog about similar woes with Sass in production that set me on the path to a solution. It seems that when Rails encounters stylesheet_link_tag calls, it starts to pull together all the stylesheets and sometimes Sass is unable to generate the CSS files fast enough. Rails then throws an exception about not being able to find the CSS files and outputs an empty CSS file to the cache path.
The solution? Generate all the CSS files from Sass templates prior to restarting Rails when deploying. I added a rake task for updating all the Sass stylesheets:
namespace :sass do
desc 'Updates stylesheets if necessary from their Sass templates.'
task :update => :environment do
Sass::Plugin.update_stylesheets
end
end
Then, I created a mirror of this as a Capistrano task:
namespace :sass do
desc 'Updates the stylesheets generated by Sass'
task :update, :roles => :app do
invoke_command "cd #{latest_release}; RAILS_ENV=#{rails_env} rake sass:update"
end
# Generate all the stylesheets manually (from their Sass templates) before each restart.
before 'deploy:restart', 'sass:update'
end
Now, whenever I do a cap deploy, the stylesheets are generated before the Rails processes are restarted, ensuring that Rails’ stylesheet_link_tag helper is always able to find the pure CSS files when trying to merge them together and caching them to a single file.
In: Mac
28 Jun 2008Something I noticed completely by accident today when I clicked on the titlebar of QuickTime Player today with the Cmd key held down. The “titlebar” is this thing here – I’m not sure that’s the right name for it:
Anyway, if you hold down the Cmd key (aka the Apple key), a menu pops up that shows the folder hierarchy of where the currently opened QuickTime movie is in your filesystem:
This works in all Mac apps that display the filename of the currently open/focused file in the titlebar.
It’s useful for me since my NADD means I try to close as many unused windows as possible to adhere to my Cmd-Tab diet – now I can close Finder windows after opening files and be sure that I can get back to them quickly. What about QuickSilver? Yup, I do use (and love) QuickSilver but I don’t let it catalog every single file!
Another nice thing about this is that I can easily reveal files in Finder in my favorite text editor (TextMate) without needing to use the project drawer:
You may have heard of it, but just in case, Living on the Edge is now going to be published on the official Ruby on Rails weblog. Big thanks to Gregg Pollack for getting me the new “gig”, and more importantly, for reviving and freshening up the content on the official Rails blog.
Catch the new first edition – this one’s about the API changes since Rails 2.1.
Day 1 of RailsConf 2008 was basically tutorial day (schedule) and started with my colleague here with me, Arun, missing out on Yoga on Rails and me sleeping until the first tutorial session. Anyway, I snapped some photos while trying to remain the unobtrusive tourist.
Here’s a shot of Portland Convention Center where it’s all happening:

There’re queues for collection of badges after registering your attendance:

Patience in the queue rewarded me with a RailsConf badge/name tag:

I was at the Meta-programming Ruby for Fun & Profit tutorial in the morning. I think when I selected the tutorial it was before I’d seen Neal Ford and Patrick Farley’s (the speakers) presentation videos from elsewhere – I know Patrick presented at MWRC and enjoyed that video.

So anyway, after the break I went over to the Refactoring Your Rails Application tutorial. Was pretty good, but I didn’t learn much I didn’t already know.
Lunch came in the form of a pretty box:

After lunch was the 2nd tutorial session and I went to both CI for the Rails Guy (or Gal) (by Chad Woolley) and Developer Testing Tricks (by Brian Takita). There were some scathing comments about how the tutorials were rather underwhelming so far in #railsconf on IRC. While I agree that the tutorials were rather underwhelming, I think I should have expected it. Oh well, I’ll know to skip them next time.
Later that night, at the Birds of a Feather session, after stealing a Pivotal Labs t-shirt (they’re launching a bug tracker, project management type app called Pivotal Tracker at RailsConf), Yehuda Katz (Merb and jQuery ninja) gave a presentation on Merb (geared towards Rails folks). It was a pretty interesting talk though there wasn’t much above what Ezra had presented previously at GoRuCo 2008 and at MWRC 2008 (I think Yehuda did one too but I can’t remember where now). Yehuda pointed out a (heated) discussion that happened recently on keeping Merb syntax as Rails-friendly as possible. I have no objection against a different syntax really, especially since Merb looks pretty well-documented in the source itself – would be nice if someone could point out an up-to-date Merb tutorial though.
Anyway, that’s it from me – as always, if anyone who reads my blog recognizes me at RailsConf, do say hi (#railsconf works too).
No mind-blowing changes in Rails this week prior to RailsConf – as Gregg mentioned last week in the Rails Envy podcast, it’s pre-2.1 days (Rails 2.1 will probably be released at RailsConf) so it’s pretty easy to see why. Oh and all Rails tests now pass in Ruby 1.9 after a long-standing #module_eval bug got fixed in Ruby 1.9′s trunk (see thread for more details).
I’ll be at (my first) Railsconf 2008 in Portland, Oregon this Thursday onwards – if anyone sees me and recognizes me from my Facebook picture please come and say hi (no head butting though).
This week’s report covers changes from 19th May 2008 to 25th May 2008 (the day the corresponding Rails Envy podcast was recorded).
Remember how the merging of the has_finder gem into Rails allowed you to do things like Post.first and Post.last?
Now you can go one step further and use the same methods on your ActiveRecord associations. For example:
post = Post.find(1)
first_comment = post.comments.first
If you have a named_scope named recent defined, you can even do this:
post.comments.recent.last
This neat little enhancement is courtesy of Ryan Bates (yes, that Ryan Bates of Railscasts fame).
Related changeset: http://github.com/rails/rails/commit/73c59638549686fccc749ffd3ac53cb533c5fd61
The cache stores in Rails (Memcache, file stores, etc.) now have an exist? method that checks whether a cached value exists given a cache key. This allows Rails controllers to expose a fragment_exist? method that allows you to check for existence of a cache fragment:
fragment_exist?('example.com/foo/bar')
This little enhancement is courtesy of José Valim.
Related changeset: http://github.com/rails/rails/commit/99860b72aebe0348f41e82d4710343498d89a84b#diff-2
You can now create records for associations like so:
post.coments.create!(:title => 'Techcrunch') do |c|
c.body = "Rails can't scale"
end
This is in keeping with the ActiveRecord::Base.create change previously mentioned.
Credit for this patch goes (once again) to Ryan Bates.
Related changeset: http://github.com/rails/rails/commit/6cba97d2a449faf21aec9fe9d4434067e414226f
As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series.