Posted by Prabu D on February 8th, 2013
Last month, I spent most of time upgrading a beautiful application from Rails 2 to Rails.
It was such a wonderful experience and I just felt to share it with everyone.
The key tools are rails_upgrade plugin and rvm.
As a first step, lets upgrade the ruby itself using rvm
Update Ruby and Install Rails 3
Use RVM to install latest version of Ruby 1.9.3 and the install Rails 3.
Run rvm update to get latest headers.
$> rvm get head
rvm reloaded ...!
Once RVM has updated it will automatically reload itself otherwise we need to do that by running below command.
$> rvm reload
Now we can use RVM to install Ruby 1.9.3.
$> rvm install 1.9.3
This command will download, compile and install the latest version of Ruby 1.9.3.
To check installed ruby version
$> ruby -v
ruby 1.9.3p327 (2012-11-10 revision 37606) [i686-linux]
$> rvm --default use 1.9.3
Now lets install rails 3
$> gem install rails –pre
Note that since we used a rvm-installed version of Ruby we don’t need to use sudo to install gems.
Now lets move to next step.
To upgrade a Rails 2.3 application to Rails 3.2.
-
Create a separate branch if the applications use Git for source control.
$> git checkout -b rails3
-
rails_upgrade ( https://github.com/rails/rails_upgrade ) – It is useful to see what changes we need to make.
Useful command :
# Check your app for required upgrades
rake rails:upgrade:check
# Backup your likely modified files that might be overwritten by the generator
rake rails:upgrade:backup
# Generate a new route file
rake rails:upgrade:routes
# Generate a Gemfile from your config.gem directives
rake rails:upgrade:gems
# Generate code for a new config/application.rb from your environment.rb
rake rails:upgrade:configuration
To install rails_upgrade plugin we first need to swap back to our Rails 2 environment.
$ rvm system
$rails2_app> script/plugin install git://github.com/rails/rails_upgrade.git
Now we can analysis by using below command
$rails2_app> rake rails:upgrade:check
Before starting to upgrade, run the command
$rails2_app> rake rails:upgrade:backup.
This will back up some of the key files that are likely to be overwritten during the upgrade.
-
We start the upgrade by creating a new Rails 3 application in the application’s
directory. In order to do that we need to swap back to our Ruby 1.9.3 environment.
$ rvm 1.9.3
$rails2_app> rails new .
Now create a new application in the same directory as an existing one, we’ll be asked if we want to overwrite some of the existing ones. If we’re not sure if a file should be overwritten we can use the d option to see the differences between the existing file and the one that will be overwritten.
The rails2_app application has now been upgraded to Rails 3.2 but we still have to go through the files that we backed up and move over any code that is specific to our application. Each backed-up file will be in its original directory with a .rails2 extension
- Finally, if the file environment.rb file configures the application’s gems move those gems into Gemfile. The syntax has changed slightly and so the code need to be changed to look like this:
Gemfile
gem 'libv8', '~> 3.3.10'
gem 'execjs'
gem 'therubyracer', '0.10.2'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
…...
now we need to run bundle install to make sure that all of the required gems are installed.
-
Try to run the rails application
$rails2_app> rails s
Gem changes for rails 3.2.X :

1. Line cache
Problem : An error occurred while installing linecache (0.46), and Bundler cannot continue. Make sure that `gem install linecache -v ’0.46′` succeeds before bundling.
Solution : To install latest linecache (manual)
curl -OL http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem
gem install linecache19-0.5.13.gem
2. Javascript runtime
Problem : autodetect’: Could not find a JavaScript runtime.
Solution : Add the below gems in Gemfile
gem 'libv8', '~> 3.3.10'
gem 'execjs'
gem 'therubyracer', '0.10.2'
3. therubyracer
Problem : An error occurred while installing therubyracer (0.11.0), and Bundler cannot continue.
Solution : Manual installation for libv8.
#> gem install libv8 -v=3.3.10
4. ruby-debug
Problem : linecache can’t handle 1.9.x yet. undefined symbol: ruby_current_thread
Solution : Modify the following gems in Gemfile
gem 'linecache19', '0.5.13'
gem 'ruby-debug19', :require => 'ruby-debug'
gem 'ruby-debug-base19x', '0.11.30.pre10'
5.ActionDispatch::Routing
Problem : config/initializers/new_rails_defaults.rb:14: undefined method `generate_best_match=’ for ActionDispatch::Routing:Module (NoMethodError)
Solution : the comments in the file new_rails_defaults.rb indicate that it should be deleted
when Rails 3 is released. To fix remove the file from initializers/.
6.ActionController::Dispatcher
Problem : uninitialized constant ActionController::Dispatcher in config/initializers/omniauth.rb file.
Solution : change the line instead of “ActionController::Dispatcher.middleware.use OmniAuth::Builder do” to “Rails.application.config.middleware.use OmniAuth::Builder do”
7. RAILS_ROOT
Problem : uninitialized constant RAILS_ROOT (NameError)
Solution : Use Rails.root instead of RAILS_ROOT constant.
8. Application controller
Problem : “Expected /…/app/controllers/application.rb to define Application.”
Solution : remove or rename app/controller/applicaiton.rb into application_controller.rb
9. request uri
Problem : undefined method `request_uri’
Solution : replace the line in the file lib/login_system.rb
def store_location
session[:return_to] = request.request_uri
end
by :
def store_location
session[:return_to] = request.url
end
10. Library
Problem : In Rails 3 /lib modules are not loaded automatically.
Solution : added the below lines in the config/application.rb file.
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
11. Use mysql2
Problem : Please install the mysql adapter: `gem install activerecord-mysql-adapter` (can’t activate mysql2)
Solution : Install the sql client package.
#> sudo apt-get install libmysqlclient-dev then in Gemfile file added gem ‘mysql2′.
12. Routing
Problem : This problem can sometimes be caused by disabling or refusing to accept cookies. redirect problem in routing.
Solution : Change the routes syntax based on rails3 update.
Hope you all liked this blog
Some useful links from railscasts
http://railscasts.com/episodes/225-upgrading-to-rails-3-part-1
http://railscasts.com/episodes/226-upgrading-to-rails-3-part-2
http://railscasts.com/episodes/227-upgrading-to-rails-3-part-3
http://railscasts.com/episodes/202-active-record-queries-in-rails-3
http://railscasts.com/episodes/203-routing-in-rails-3
343 522
Follow responses at RSS 2.0.
Leave a response | Trackback.
Leave a Reply
Nice article . Thanks