DAVID RICE

Software Developer

+44 (0) 7590 538 303

46 Hill Street
Belfast, Northern Ireland
BT1 2LB

How to Upgrade to Rails 3.1.0

25 May 2011 {View Comments}

The [Rails 3.1.0 Release Candidate][0] dropped a few days ago and thought I'd give it a try on an application I was busy upgrading already. Before starting you'll probably want to check out the Release Notes or the [railscasts overview video][1] first. ## 1. Upgrade the Gemfile source 'http://rubygems.org' gem "rails", "3.1.0.rc1" gem "mysql2", "0.3.2" # Rails 3.1 - Asset Pipeline gem 'json' gem 'sass' gem 'coffee-script' gem 'uglifier' # Rails 3.1 - JavaScript gem 'jquery-rails' # Rails 3.1 - Heroku group :production do gem 'therubyracer-heroku', '0.8.1.pre3' gem 'pg' end Needed specifically the latest version of mysql2 as the earlier ones are incompatible. To get the asset pipeline working on heroku you'll need *therubyracer-heroku*. Another issue with heroku, it appears you now need to be explicit about using postgres in production. ## 2. Config File Changes ### Update config/boot.rb require 'rubygems' # Set up gems listed in the Gemfile. ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) ### Update application.rb In *config/application.rb* add the following line. # Enable the asset pipeline config.assets.enabled = true ### Update development.rb I seemed to have to remove the following line from *config/environments/development.rb* config.action_view.debug_rjs = true ### Update production.rb Add the following lines to enable asset compression in *config/environments/production.rb*. # Compress both stylesheets and JavaScripts config.assets.js_compressor = :uglifier config.assets.css_compressor = :scss ## 3. Move Assets ### Move the Folders mkdir app/assets git mv public/images app/assets/images git mv public/javascripts app/assets/javascripts git mv public/stylesheets app/assets/stylesheets ### Fix Image References Use your favourite find and replace, check judiciously. find: /images/ replace: /assets/ ### Make CSS and JavaScript Manifest Files I already had an *app/assets/application.js* file so I added this to the top of mine and moved the code in there out into separate files with app/assets/javascripts. // This is a manifest file that'll be compiled into including all the files listed below. // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically // be included in the compiled file accessible from http://example.com/assets/application.js // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // the compiled file. // //= require jquery //= require jquery_ujs //= require_tree . I did the same for *app/assets/application.css* /* * This is a manifest file that'll automatically include all the stylesheets available in this directory * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at * the top of the compiled file, but it's generally better to create a new file per style scope. *= require_self *= require_tree . */ ### Fix the CSS and JavaScript references Instead of requiring multiple files you'll want to require the manifest files created above. <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> ### Success! On an app with good front end conventions established, already using a method of asset bundling, etc. this isn't a big task. However I do find that because Rails has set such good conventions it's hard for people to go too wrong. Of all the applications where I've **taken over** from another developer over the years. Rescue missions, etc. It's the prospect of looking in the *public/* directory that scares the bajaysus out of me. Thankfully with all this great new front end stuff I'm hoping that in the future, rescue missions into interface land will suffer less casualties. [0]: http://weblog.rubyonrails.org/2011/5/22/rails-3-1-release-candidate [1]: http://railscasts.com/episodes/265-rails-3-1-overview
«