How to play with Rails 3.1 on Ubuntu with RVM

Rails core team recently confirmed that Rails 3.1 will ship by default with jQuery, SASS and CoffeeScript. While Prototype replacement with jQuery is not controversial, this is not the case for SASS (a framework to DRY up your stylesheets) and CoffeeScript (a cleaner syntax on top of Javascript). Everything is explained here, and you can look at all the smart comments on the github commit.

RubyInside blog also published an excellent tutorial on how to play with Rails 3.1 right now. Read it first, I will not explain everything here, but I had 2 problems following the how-to.

First, install rails master branch (I use the awesome hub wrapper for git) :

cd ~/code
git clone rails/rails

Then create a gemset with RVM :

rvm use 1.9.2@yourapp --create
gem install i18n thor bundler

Then we can create our rails app:

./rails/bin/rails new yourapp --edge
cd yourapp

Unfortunately, I had 2 problems with this application.

First, the rails binary installed in $GEM_HOME/bin is not fully functional :

% rails -v
/usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/rubygems.rb:762:in `report_activate_error': Could not find RubyGem rails (>= 0) (Gem::LoadError)
      from /usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/rubygems.rb:219:in `activate'
      from /usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/rubygems.rb:1065:in `gem'
      from /usr/local/rvm/gems/ruby-1.9.2-p180@yourapp/bin/rails:18:in `<main>'

The simplest way to solve this is a shell alias:

% alias rails=$GEM_HOME/bundler/gems/rails-*/bin/rails
% rails -v
Rails 3.1.0.beta

With this, I could make the application run, with SASS support, good.

But CoffeeScript didn’t work. When I tried to introduce an app/assets/javascripts/awesome.js.coffee script, nothing happened, no alert. I had the following error instead of a normal application.js file:

ExecJS::RuntimeError
Could not find a JavaScript runtime

The error is thrown by the ExecJS gem. It tries to find the best javascript runtime among those listed in the README. There’s at least one included in MacOS X, but as a Ubuntu user, I have none. I tried to install therubyracer and mustang, but 1/ it took a very long time (compilation of native code on a netbook), 2/ it led me to other errors I had not been able to understand. The simplest way I found to get a working JS runtime is the nodejs package of the distribution:

% sudo aptitude install nodejs

Restart the Rails server, and everything should work smoothly now!

You can forget the advices in the CoffeeScript section, the application.js is already good with //= require blah at the beginning.

One last thing, you should note this Rails version is pre-beta. Some things may not work. For instance, the scaffold generators gives you an app/assets/javascripts/<controller>.js.coffee with 3 comment lines starting with // instead of #, resulting in this kind of errors:

Error: Parse error on line 1: Unexpected 'MATH'

Don’t know if it’s a Rails or ExecJS bug though. But the conclusion is the same: use with caution.

I’m really excited with the upcoming features in Rails 3.1, particularly automatic reloading and engines support. It was pretty fun to see all this rage around CoffeeScript and SASS, both seem nice, productive, and are easy to disable. I can’t wait to try a RC version !