How to Upgrade/update Rails 3.0.x to 3.1.x

Recipe # | posted in Howto, Linux, Ruby/Rails | Comments

1 – Problem Description

How to upgrade/update Rails 3.0.x to 3.1.x

2 – Solution

Anxious to upgrade? No worries, here are the steps after a painful weekend :)

  1. Delete your Gemfile.lock from your app root folder and let bundler manage the new dependencies

    cd path/to/my_rails_app rm Gemfile.lock

  2. Remove all version strings from your gems and let bundler figure out the dependencies. For example,

    gem ‘mysql2’, ‘0.2.17’ gem ‘devise’, ‘1.5.2’

should be

gem 'mysql2'
gem 'devise'

Update rails gem version and add support for the asset pipeline,

gem 'rails', '3.1.4'

# Asset pipeline
gem 'sass-rails'
gem 'coffee-rails'
gem 'uglifier'
gem 'therubyracer'
gem 'jquery-rails'
  1. Before executing the following command make sure you have a backup of your app, as this will overwrite some files. Later you should get a diff and add your extra code in the new files.

    bundle exec rake rails:update

Let’s double check now that everything is in place. If not add or remove accordingly.

  1. In config/application.rb add
1
2
3
4
5
6
7
8
9
10
11
12
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end

# Enable the asset pipeline
config.assets.enabled = true

# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'

and remove

1
2
3
4
5
6
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)

# JavaScript files you want as :defaults (application.js is always included).
config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
  1. In config/environments/development.rb add
1
2
3
4
5
# Do not compress assets
config.assets.compress = false

# Expands the lines which load the assets
config.assets.debug = true

and remove

1
config.action_view.debug_rjs = true
  1. In config/environments/production.rb add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false

# Compress JavaScripts and CSS
config.assets.compress = true

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

# Generate digests for assets URLs
config.assets.digest = true

# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH

# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
  1. In config/environments/test.rb add
1
2
3
# Configure static asset server for tests with Cache-Control for performance
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
  1. Move folders public/images, public/stylesheets and public/javascripts according to the official rails guide asset organization [1]:
    Pipeline assets can be placed inside an application in one of three locations: **app/assets**, **lib/assets** or **vendor/assets**.
    **app/assets** is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.

    **lib/assets** is for your own libraries’ code that doesn’t really fit into the scope of the application or those libraries which are shared across applications.

    **vendor/assets** is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.
  1. Create css and javascript manifest files

    vi app/assets/stylesheets/application.css

    and add
    
    /*
    • 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 . */

    vi app/assets/javascripts/application.js

and add

// 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 your_js_file
//= require jquery
//= require jquery_ujs
//= require_tree .

    Note here that in Rails 3.1, **jquery-rails** gem provides the **jquery.js** and **jquery_ujs.js** files via the asset pipeline. You won’t see them in the asset folders!

Now, go and change all app/views files to include

1
2
<%= stylesheet_link_tag    "application" %>
<%= javascript_include_tag "application" %>

instead of requiring multiple .css and .js files.

  1. . Fix image references by replacing all

to

<%= image_tag "blahblah.png" %>

Hopefully executing

bundle exec rails s

should work now!

Some final tips.

  1. In case you change to production environment, first precompile the assets by executing

    bundle exec rake assets:precompile bundle exec rails s

    1. If you have gem ‘delayed_job’ in your Gemfile, you should change it to gem ‘delayed_job_active_record’

    2. If you have gem ‘devise’ in your Gemfile, you should have a drink and go read the upgrade instructions [2] :P

Give me a hug :D

3 – References

[1] Asset pipeline

[2] How To: Upgrade to Devise 2.0

Comments