OS: Linux Debian Wheezy
Rails version: 3.2.0
I want to configure RSpec to work with Rails.
I'm using this instruction: https://github.com/rspec/rspec-rails ('Installation' section).
When I try:
rails generate rspec:install
after successfully performing all the things described in the manual before,
I get:
Usage: rails new APP_PATH [options]
etc.
How to resolve this?
IMHO Relevant part of my Gemfile:
gem 'rails', '3.2.0'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '~> 2.0'
end
(I'm new to Rails and using M. Hartl manual to learn.)
(What I have tried already:
changing Rails version (4.0.0, then to 3.2.0);
changing rspec version (initially 2.8.0, then to ~> 2.0);
a few times 'bundle update', 'bundle install' and 'bundle update rspec-rails';
installing 'rspec' and 'rspec-rails' using 'gem install';
This is because when you install gem install rails after June 25, 2013 - it will install the rails 4. When you following the course, Hartl asks you to copy his Gemfile located in his GitHub repo which actually refers to Rails '3.2.14'.
To solve this problem, create a gemset using RVM.
rvm gemset create sample_app
rvm gemset use sample_app
Then create a Gemfile inside an empty folder.
mkdir sample
touch Gemfile
Copy the content form the Hartl's (Gemfile)[https://raw.github.com/railstutorial/sample_app_2nd_ed/master/Gemfile] to the Gemfile you just now created and run,
bundle install
Once all gems are installed, simply go outside the folder and delete it.
cd .. && rm -rf sample
Now create a new Rails project and skip the auto bundle install.
rails new sample --skip-unit-test --skip-bundle
Once Rails generated the files, go inside the folder and replace the Gemfile contents again with Hart's Gemfile.
Now run bundle install
Finally you can create the RVM file that will automatically switch to the project gemset.
rvm --create --ruby-version use ruby-2.0.0#sample-app
ruby-2.0.0 refers to my system's active Ruby version. You'll need to replace this accordingly.
This should work fine. Let me know if you have any further help.
Cheers!
Related
I have a problem with two vendored gems of a Rails 3.2 app. One gem is a Minitest extension, the other a piece of code which should use the Minitest extension in it's test suite. Since both are under development, both are vendored and not yet pushed to Rubygems.
To reproduce the issue, I've bootstrapped a bare Rails 3.2 app and added two gems to vendor/gems: minitest-great_expectations (a working Minitest extension cloned from Github) and mygem which only reports and tests it's VERSION.
First let's try with the minitest-great_expectations gem from path:
git clone https://github.com/svoop/minitest_sandbox.git
cd minitest_sandbox/vendor/gems/mygem/
cat Gemfile # note "path:"
bundle install # note "Using minitest-great_expectations (0.0.5) from source at ../minitest-great_expectations"
ruby test/lib/mygem/version_test.rb # => test_helper.rb:6 - cannot load such file -- minitest/great_expectations (LoadError)
And now the same with the minitest-great_expectations gem from Rubygems. Edit the Gemfile and use the line without the "path:".
$EDITOR Gemfile # remove the "path: ..."
bundle install # note "Installing minitest-great_expectations (0.0.5)"
ruby test/lib/mygem/version_test.rb # => PASS
Reverting back will now work since the gem is installed. However, it doesn't matter whether the Gemfile contains the "path:" or not. Explicitly uninstall the gem with gem uninstall minitest-great_exceptions and you're back to LoadError.
Any idea what's missing here?
You should be running bundle exec ruby test/lib/mygem/version_test.rb.
Path gems are a Bundler-specific extension to Rubygems, so bundle exec is necessary to set up the load path correctly.
I create a gemset with
$ rvm gemset create r3
$ rvm gemset use r3
$ gem install rails -v3.2.13
At this point
$ rails -v
now shows
$ Rails 3.2.13
but every time I do bundle with a project I've forked, I find that
rails -v
shows Rails 4.0.1 - which then gives issue with the project in question when running tests
(4.0.1 conflicts with 3.2.13).
My question is - if my Gemfile only has:
$ cat Gemfile
source 'https://rubygems.org'
gemspec
how is this happening? How can I make my command line ruby version stay at 3.2.13 and not switch to 4.0.1 Once it switches to 4.0.1 I seem to be stuck with that for that gemset and to create a 3.2.13 gemset I have to start over again.
Bundle works by finding the most up-to-date version of the gems that are compatible with the restrictions from the Gemfile. In this case, those restrictions are coming from the gemspec file, which presumably allows versions of Rails greater than 3.2.13. So it's picking the most up-to-date version of Rails allowed - which is 4.0.1. Your RVM configuration is not relevant here.
To lock your particular fork to Rails 3.2.13, just add the following:
gem 'rails', '3.2.13'
to the Gemfile in your fork. This will lock the local version to 3.2.13 when you run bundle.
if you wish to explicitly use different version of rails then the one that would be calculated from Gemfile then you need to use:
NOEXEC_DISABLE=1 rails ...
you can make it permanent for single shell session with:
export NOEXEC_DISABLE=1
rails ...
and to disable loading gems in versions calculated via Gemfile put it in you shell initialization file (like ~/.bash_profile or ~/.zlogin):
export NOEXEC_DISABLE=1
this happens because RVM installs gem rubygems-bundler which automatically analyzes your Gemfile when you run gem binaries and if a version specified via this file is available then it is loaded (even if it is specified only as dependency of your gems).
I've set up an RVM environment and installed Bundler and Rake.
In my Project I've got a Gemfile, when I run bundle install the gems get installed in the project folder that I've specified. But when I run gem list I don't see the gems installed by Bundler.
Why is that?
Thanks for your help
Edit:
And also I've definied the rails gem and it's version in the project Gemfile so I never ran gem install rails
Edit 2:
I've ran gem install rails. When I run rails -v outside my project I get 3.2.12, inside my project folder I get 3.2.11 which is the version defined in my Gemfile.
But why? Why did I have to install Rails globally?
In your .rvmrc write following code:
rvm use <ruby_version>#<project_name> --create
In my case ruby_version is ruby-1.9.3-p194
Once you navigate to your project path, run bundle install to install all the gems specific to your project.
Trying to reinstall RSpec, and I can't seem to re-add its command to my bin folder.
Mac-Users-MacBook-Pro:bin macuser$ rspec
-bash: /usr/bin/rspec: No such file or directory
>> which rspec
#> returns nothing.
I tried sudo gem install rspec --prerelease a dozen times, bundle install , and nothing seems to give.
What am I missing?
Hey Trip, I'm pretty sure the command is spec and not rspec.
Edit
Ok, so, I took another look, you are right the rspec command is included in rspec2...
So, here is a Gemfile I have which properly installs rspec w/ bundle install
source 'http://rubygems.org'
gem 'rails', '3.0.0'
gem 'mysql'
group :development, :test do
gem "rspec-rails", ">= 2.0.0.beta.17"
end
In my case, I'm using rvm & it is installed to:
/Users/me/.rvm/gems/ree-1.8.7-head#<my gemset>/bin/rspec
I went ahead and uninstalled all of rspec2.
Then instead of sudo gem install rspec --prerelease, I did sudo gem install rspec-rails --prerelease which worked.
Then I followed the explicit directions to delete certain files that were floating around in previous beta releases. Everything is copacetic.
I have two versions of rails (2.1.0 and 2.2.2) installed in my computer.
When I create a new application, is it possible to specify that I want to use the older (2.1.0) version?
I found here an undocumented option to create a new application using an older version of Rails.
rails _2.1.0_ new myapp
Here is the command which I use normally:
rails _version_ new application_name
for example rails _7.0.4_ new my_app
Here is the list of all available rails versions so far:
http://rubygems.org/gems/rails/versions
I was having some trouble using rails _version_ new application_name (the resulting project was still generated for the newest version of Rails installed.)
After a bit of digging I found an article by Michael Trojanek with an alternative approach. This works by creating a folder with a Gemfile specifying the desired version of Rails and then using bundle exec rails... so that Bundler takes care of running the appropriate version of rails. e.g. to make a new Rails 4.2.9 projects the steps are:
mkdir myapp
cd myapp
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '4.2.9'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
bundle update
As rightly pointed out by #mikej for Rails 5.0.0 or above, you should be following these steps:
Create a directory for your application along with a Gemfile to specify your desired Rails version and let bundler install the dependent gems:
$ mkdir myapp
$ cd myapp
$ echo "source 'https://rubygems.org'" > Gemfile
$ echo "gem 'rails', '5.0.0.1'" >> Gemfile
$ bundle install
Check that the correct version of rails has been installed: $ bundle exec rails -v
Now create your application, let Rails create a new Gemfile (or rather overwrite the existing one by using the --force flag) and instead of installing the bundle (--skip-bundle) update it manually:
$ bundle exec rails new . --force --skip-bundle
If you check the entry for rails in Gemfile, it should be like this:
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
You should update it to the exact version needed for the application:
gem 'rails', '5.0.0.1'
Now, the final step:
$ bundle update
There are two ways to achieve this:
one as suggested in accepted answer:
gem install rails -v 2.1.0 #only when the gem has not been installed in the desired ruby version you are using, so that you don't get error on next step
rails _2.1.0_ new my_app
and alternative method is to create gemfile with desired rails version before initializing rails project
mkdir my_app
cd my_app
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '2.1.0'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
I have written about this in details in my article
You can generate the skeleton with either version and require the one you want in config/environment.rb:
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.1.2' unless defined? RAILS_GEM_VERSION
or use the "rails" command form the version you want anyway.
You should also take a look at "freezing" your Rails gems into the app. This helps a lot with deployment, specially in shared hosting environments.
Just change the RAILS_GEM_VERSION variable in config/environment.rb and issue the freeze rake task:
rake rails:freeze:gems
Please watch out which version of ruby you are using with Rails.
The command for making a new project for a specific version of Rail may not work for you. I had some issues about it. And the problem was the ruby version I have default which is 3.0.0. This version did not work with Rails 5. Then I installed ruby 2.7.5 and switched to it as default. Only then I was able to make projects both for Rails 5 and 7.
If you want the same environment with ruby 2.7.5
rvm install ruby-2.7.5
switch to this version as default
rvm --default use 2.7.5
install bundler and webpacker
gem install bundler
gem install webpacker
install lastest rails (which is 7)
gem install rails
test it
rails new test_app_6
cd test_app_6
rails s
check for localhost 3000
http://localhost:3000
then stop the server (control + c) and install Rails 5
gem install rails -v 5.2.6
test it
rails _5.2.6_ new test_app_5
cd test_app_5
rails s
check for localhost 3000
http://localhost:3000
You're set!