Can I change the expected Rails version of a Rails application? - ruby-on-rails

I downloaded an archive containing code for a Rails application ( from a book I'm reading ). I'm running Rails 3.0.1 but the application fails to start, because it's looking for 3.0.0beta3. Is there some way of starting it with my version of Rails?

Yes, look for the Gem file in the application root and look for the line
gem 'rails', '3.0.0beta3' and change it to
gem 'rails', '3.0.1'
then run bundle install from a terminal in your application root.

this is because your Gemfile.lock is probably showing Rails 3.0.0.beta3. Even if you change your Gemfile to 3.0.1 AND install it using gem install rails, you will need to run bundle install or bundle update rails

There is probably a line in the environment.rb file that looks like
RAILS_GEM_VERSION = '3.0.0beta3' unless defined? RAILS_GEM_VERSION
Updating this should sort your issue.

Related

How do I update rails to 4.2?

How do I update rails to 4.2?
I googled it.I have read 3 or 4 so questions about it. But I am confused still. May be because I am new to rails.
Right now I got that the best way is just to specify rails version in my gem file.
Update my rails version
jonstark#jonstark-pc:~/rails_projects$ rails -v
Rails 4.0.3
And I want to make latest uptodate rails version default
It depends on your Rails version. In your case it hasn't changed that much on the app side from 4.0.3 to 4.2.5.1. Nonetheless I would recommend you to run the update script of rails afterwards.
Important! Don't forget to commit. Before and after.
Step 1:
Update Rails in your Gemfile:
gem 'rails', '4.2.5.1'
Run bundle update.
Step 2:
Run the update script:
$ rake rails:update
Be careful! It asks you to override a lot of config files. Check the changes and decide if the changes are necessary.
Step 3:
Check your Gems if all of them are compatible.
Here also an Upgrading Guide from the Rails Guides.
Edit your Gemfile, change the existing rails specification:
gem 'rails', '~> 4.2', '>= 4.2.5'
Run bundle update rails

Specifying rails version to use when creating an api (rails-api)

I would like to create a rails-api with a specific version number for rails (i.e. 4.0.5) I would like to know how to proceed through the command line. Up to now I proceeding as if I would have to specify a rails version for an app but I'm getting some obvious error that it's not the way to proceed
rails-api _4.0.5_ new application_name -d mysql
error
Could not find 'rails-api' (= 4.0.5) - did find: [rails-api-0.2.1] (Gem::LoadError)
I think you need to install rails the standard way:
gem install rails -v=4.0.5
I am not familiar with rails-api but looks like it will only accept the rails-api version as a parameter, not rails itself. If it creates a rails app itself, it will certainly create a Gemfile. You could always change the rails version in the gemfile later and run bundle again:
gem 'rails', '4.0.5'
And run
bundle install

How can I generate older rails application when current version is 3.0.3

I have 2 different Rails Rubygems installed on my Linux OS [2.3.8 and 3.0.3], but the current active Rails version is 3.0.3. So is it possible to generate Rails app using 2.3.8 style generator:rails testapp not new one: rails new testapp?
You can tell Rails to use version 2.3.8 like this:
rails _2.3.8_ testapp
I'd recommend installing RVM and setting up different gemsets for each version of Rails. That way you can easily switch between Rails versions by just switching gemsets.
See here: How to use the Rails 2.3 app generator when I have Rails 3 installed?
Quoting Leonid Shevtsov:
The easiest way to do it was:
Create the directory for the project
Create a Gemfile there containing
gem "rails", "2.3.9"
gem "sqlite3-ruby", :require => "sqlite3"
Run "bundle install"
Run "bundle exec rails ." to create an app in the current path
You don't even need rvm to do this.
You could also use rvm.

How to use the Rails 2.3 app generator when I have Rails 3 installed?

to clarify: there's only one rails command, which gets installed from the latest Rails gem, which is Rails 3 ATM. However, I'm required to create a Rails 2.3 app.
Running ruby /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.8/bin/rails fails with a NoMethodError, I suppose because it also tries to use gems from the 3.0.0 release.
Uninstalling the gem produces some strange results:
$ gem uninstall rails-3.0.0
ERROR: While executing gem ... (Gem::InstallError)
cannot uninstall, check `gem list -d rails-3.0.0`
$ gem list -d rails-3.0.0
*** LOCAL GEMS ***
(and no gems here)
What should I do?
The easiest way to do it was:
Create the directory for the project
Create a Gemfile there containing
gem "rails", "2.3.9"
gem "sqlite3-ruby", :require => "sqlite3"
Run bundle install
Run bundle exec rails . to create an app in the current path
You don't even need rvm to do this.
(I assume 2.3.11, given it's the latest)
rails _2.3.11_ new app will do this for you without you having to muck about.
(Had to make a comment since I don't have enough Stack Overflow cred and can't directly respond to answers yet.)
For folks running rails 3 now the "new" command is now required for creating new rails applications. As such "new" will need to be appended to the end of the commands.
So for Leonid Shevtsov's answer, Step 4: bundle exec rails new .
And for Robert Speicher's answer: rails new .
Install rvm and then create a new gemset, so that Rails 2 is isolated.
Or, go to the directory where you want your Rails 2 app to be, create a Gemfile like a Rails 3 app, but specify gem "rails", "~> 2.3" and run bundle install, and you should now be able to issue rails .

How can my Rails app accept RAILS_GEM_VERSION minor version bumps

My rails project has this line in /config/environment.rb
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
As we now have 2.3.5 as the most recent upgrade, is there a way to make my environment.rb accept minor version bumps?
(without I have to explicitly change 2.3.2 to 2.3.5)
No, there isn't.
You application needs to use a specific Rails version mostly because different tiny releases might require additional steps to upgrade the framework such as changes to boot.rb.
$ rake rails:update
Things have evolved a bit since Rails 2, so Ill share what I had to do to get from 5.0.0 to 5.0.0.1 today.
My Gemfile read gem 'rails', '~> 5.0.0'. I figured that was enough, but bundle install was not updating anything new. So I tried to force it with gem 'rails', '~> 5.0' which also did nothing new when I ran update (note: this is for an experimental app of my own, and not someone else's app I am working on - don't just default to allowing minor version updates to solve problems like this ;) ). So I had to try a few other ways to force this security patch/hotfix.
First, I had to install the package locally:
gem install rails --version 5.0.0.1
Next, I updated bundler:
bundle install
...and I saw this in the output: Using rails 5.0.0.1 (was 5.0.0)
When I ran ./bin/rake rails:update, it wiped the contents of my config/routes.rb file, changed many of my settings in various config files (some of which were dangerous security settings to change), among a few other seemingly benign changes. While this is the expected behavior, I am pointing this out as not exactly a desirable method for updating a minor patch/hotfix for rails.
Firstly, you need to change the version to 2.3.5 from 2.3.5 and then run
rake rails:update

Resources