Migrate rails-bestpractices.com to rails4

14 Jul 2013

These 2 weeks I migrated rails-bestpractices.com to rails 4 from rails 3.2.13. Here are some experience I'd like to share with you.

Make sure you have good test code

rails-bestpractices.com has many rspec and cucumber test code, they can find out most of warnings and errors after migration.

Update Gems

First, update rails to 4.0.0 in Gemfile, but soon you will find you have to update many gems, devise, compass-rails, cucumber-rails, etc., some are rc version or raisl4 branch,

You also need to remove some gems, like strong_parameters and turbo-sprockets-rails3.

Update bin executables

Rails 4 app finds executables in bin/ directory, run

rake rails:update:bin

to get bin/bundle, bin/rails and bin/rake

Remove unused configs


Add new configs

config.eager_load = false

to config/environments/development.rb and config/environments/test.rb

config.eager_load = true

to config/environments/production.rb

New secret_token

Rails 4 encrypts the contents of cookie-based sessions, need to use secret_key_base instead of secret_token.

Application.config.secret_token = 'xxx'
# =>
Application.config.secret_key_base = 'yyy'

Remove assets group

Rails 4 has removed assets group, you should remove it from Gemfile and config/application.rb

# Gemfile
group :assets do
  gem 'sass-rails'
  gem 'coffee-rails'
  gem 'uglifier'
# =>
gem 'sass-rails'
gem 'coffee-rails'
gem 'uglifier'

# config/application.rb
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)
# =>
Bundler.require(:default, Rails.env)

Filter parameters in initializer

Rails 4 prefer setting filter_parameter in initializer.

# config/application.rb
config.filter_parameters += [:password]
# =>
# config/initializers/filter_parameter_logging.rb
Rails.application.config.filter_parameters += [:password]

Fix routes

Rails 4 doesn't allow using match (without setting via get or post), should use get or post instead, like

match '/auth/failure' => redirect('/')
# =>
get '/auth/failure' => redirect('/')

New scope syntax

Rails 4 only allows scopes as a proc

scope :published, where(:published => true)
# =>
scope :published, -> { where(:published => true) }

Enable turbolinks

Assume you are not using any client side MVC framework, like Backbone or Ember, turbolinks can speed up your web pages initialization.

1. add turbolinks gem in Gemfile

gem "turbolinks"

2. require turbolinks in application.js

//= require turbolinks

Please let me know if you have any problems to migrate to rails 4 :-)


blog comments powered by Disqus