Generating rails app in previous version after rails 3.0 beta

Rails 3.0 beta was released 2 weeks ago, rails 3 has many improvement over previous version of rails. It’s also the first version that after rails and merb merged. Truth be told, rails 3 beta has changed a lot since version 2.3.5. Many gems has not supported in rails 3 beta yet, e.g. RSpec and Cucumber. It’s hard to move your existing project to Rails 3 without the support of required gem.

As a Rails programmer, you should already grab a copy of Rails 3 beta to learn the future development of Rails. I have already installed Rails 3 beta on my Mac. Rails 3 run smoothly on new project, except RSpec and Cucumber not supported. I was able to work on existing project on Rails 2.3.5 after installed Rails 3 beta, nothing weird has happened except when I want to start a new Rails 2.3.5 project.

MyMac:work stevew$ rails _2.3.5_ rails2project
/usr/local/lib/ruby/site_ruby/1.9.1/rubygems.rb:777:in `report_activate_error': 
RubyGem version error: railties(3.0.0.beta not = 2.3.5) (Gem::LoadError)
	from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems.rb:211:in `activate'
	from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems.rb:1056:in `gem'
	from /usr/local/bin/rails3:18:in `'

What’s wrong with my rails 2.3.5? It turns out is the problem of railties gem installed by rails 3 beta, railties gem doesn’t exists before rails 3 beta, so it complain railties version 2.3.5 not found. I start google around, and I am not alone, there is a few people out there have the same problem, and I finally found the temporary solution in the web, but I forget the link. So here is the soltion:

edit your rails script, my rails is installed at /usr/local/bin/rails, change the last 2 line to

#gem 'railties', version
#load Gem.bin_path('railties', 'rails', version)
 
gemname = 'railties'
gemname = 'rails' if version.split(".").first.to_i == 2
gem gemname, version
load Gem.bin_path(gemname, 'rails', version)

Basically, we only replace the gem name of railties to rails if the version params start with 2.

  1. Thanks, that was simple yet very helpful :)

  1. No trackbacks yet.