Local copy of Rails in a rails application - ruby-on-rails

I recently upgraded a rails project I am working on from 2.0.5 to 2.3.2. I noticed that there was a local copy of the 2.0.5 rails files in vendor/rails and I was wondering should I put a local copy the 2.3.2 rails files in there too or just leave them out? What is considered a better practice?

Yes. The copy of Rails that sits in vendor/rails is actually used in preference to the Rails gems installed system-wide—in other words, though you upgraded your Rails install, your app is actually still running on 2.0.5.
The vendor/rails directory exists so you can "freeze" your app to a specific version of Rails, thus making it less vulnerable to changes in the configuration of the machine it's running on. This is so darned useful that there's an automated way to manage the directory. To delete the existing version of Rails sitting in vendor/rails, go to the root of your Rails project directory and do the following:
rake rails:unfreeze
To then install the most current Rails gems on your system into vendor/rails, do:
rake rails:freeze:gems
There are a few other things you can do with vendor/rails. Check out rake -T for a full list of commands.
P.S. If you ever hear someone talk about their Rails install being "vendored", this is what they mean.

In the meanwhile things have changed a little.
rake rails:freeze
and
rake rails:unfreeze
are deprecated. Instead you should use:
bundle install --path vendor/bundle
and
bundle install --system
to switch back.

Related

copies a rails app to a new computer: rails command not found

I have just set up ruby and rails on a freshly installed freeBSD. It works great. I can do rails new blabla mv into blabla and do rails s and it just works.
I had an up and running rails app on another computer. I copied the repertory that contained the app to the new computer running freeBSD. However, when I mv into that repertory of the rails app and to rails server I get -bash: rails: command not found
I have tried creating a new app with the same name and let rails create all the repertories and then copy the files of the existing rails app therein but no success.
I can run rails -v and rails s from within any repertory and get correct answer, but when I move into that specific repertory I get that command not found.
You're probably using a ruby version manager I suspect (rbenv/rvm)? Check the ruby version in your Gemfile (top of the file). It probably isn't the same as the ruby version you have installed. If you're using one of the above mentioned version managers than install the correct ruby for your rails project. After that you can do a gem install bundler in the project directory and after run bundle install which will install rails and all dependencies.
Which version you are using? It happens with earlier versions. Try updating your gems and bundler. And try again. Hope it helps.
Did you try running bin/rails s? I think tha you need run bundle install too.

Bundler is using a binstub that was created for a different gem.

When I run a rails console using
rails console
everything is fine.
When I run a rails console using
bundle exec rails console
I get the following warning
Bundler is using a binstub that was created for a different gem.
This is deprecated, in future versions you may need to `bundle binstub my_gem` to work around a system/bundle conflict.
my_gem happens to be a gem that I've created that is completely unrelated and not used in the current project directory.
I've tried every solution in this question with no luck:
Bundler is using a binstub that was created for a different gem
I would appreciate any guidance on removing this warning or help understanding how binstubs work so that I can figure out what's going on.
Nowadays it's common for projects to have "specialized" versions of tools. E.g. in some projects the "rails" command may be expected to be run using "spring" (to start up faster).
So it's not uncommon to generate files in your project's 'bin' directory, and then use those versions when running commands, so e.g. instead of
bundle exec rails console
or
bundle exec spring rails console
you could simply expect the following to work correctly
bin/rails console
and not care whether the project needs spring or bundler or zeus or whatever.
So if you don't have 'bin/rails' in your project, you should generate one that suits the project, e.g. using
bin/rake rails:update:bin
If you don't already have bin/rake, you might have to use
bundle exec rake rails:update:bin
(so your bin/rake commands will also get a speedup from using spring)
Some people even put ./bin in their paths, so whenever they run rake (or whatever) they are actually running ./bin/rake if it exists.
Troubleshooting
for project specific tasks, use bin/* files, creating them if needed (e.g. using special rake tasks like in Rails or using bundle binstub <gemname>) - usually those have Bundler specific lines that will make Bundler happy.
for non-project gems (like your gem), find out where it is (e.g. which mygem) and check out it's contents - it's probably using e.g. "bundler/setup" which is confusing Bundler (because bundler expects a local Gemfile file). Maybe your gem is using bundler (it shouldn't if it's a "global" kind of tool and not a "project" tool).
Also, if you're using them, check if tools like RVM and .rbenv are correctly adding their stuff to your bin files (they usually need to setup specific paths)
If you still have questions, it's best to post the contents of the bin file causing problems - it's meant to be a plain Ruby file, so if there's something wrong, it's usually because of the file contents (and not anything else).
More info: https://github.com/sstephenson/rbenv/wiki/Understanding-binstubs
It happened in a project of mine. Because I ran bundle install with another ruby version.
Make sure your rvm is the correctly ruby version.

Why "Rails -v" and "Bundle exec rails -v" is different

I have staging and productions servers through AWS. After a certain point of time(I think it was after I upgraded ruby version to 2.1), my staging/production servers couldn't find rails so I had to ssh in and redownload Rails and Ruby, but now when I run "rails -v" it gives me 2.3.14, but when I run "bundle exec rails -v" I get the proper 3.2.16.
I'm guessing this is because of paths, but not entirely sure how to solve this. Some of the answers I found suggested changing the .bashrc and changing the $PATH variable, but wasn't exactly sure what to change it to.
This is my .bashrc:
#PATH=$PATH:$HOME/.rvm/bin # how it was before
PATH = $PATH:$HOME/.rvm/gems/ruby-2.1.0/bin #edited this just now to see if it works. it didnt..
bundle exec rails will use the rails version defined in your Gemfile. On the other hand, a simple rails will run the latest version available on your computer.
What probably happened is this:
you had a rails version from rvm that was the same as the one used in your Gemfile (3.2.16), so you were not noticing any difference. After upgrading ruby, you have to reinstall all gems you did install on your previous version. Otherwise it will fallback to the latest available, which can be the system version (ie. not from rvm). This may explain why you have such an outdated one.
Note that a proper bundle install will install all the gems required in your Gemfile and then will do the job for you.

Using specified version of rails when starting?

I have multiple version of rails installed. But when I start the server with "rails s" it always uses the latest one. Is there a way to specify the version of rails (and all dependent gems) that should be used? (And how would I specify that in Pasenger?)
Yes, I could use RVM or similar - but this I would like to avoid.
Thanks in advance!
Phusion Passenger author here.
The version of Rails that is loaded does not depend on 'rails server', and also does not depend on Passenger. RVM also has got nothing to do with it.
It only depends on your Gemfile.lock, which locks down all gem versions in your project. If you want a different version of Rails, you need to modify your Gemfile to specify that exact version, and run bundle install to update Gemfile.lock.
You can't do that with rails server command. Every project depend from a GemFile. But you can create a bash script that will move a custom specify GemFile to the default GemFile then start the server.
vi /script/rails4_to_rails3
#/bin/bash
mv /opt/web/my_app/GemFile /opt/web/my_app/GemFile.rails4
cp /script/GemFile.rails3 /opt/web/my_app/GemFile
bundle install
rails s
I'm not a Passenger Expert but in my opinion it's impossible to.

Rake 10.0.4 not found in sources

I've just pushed a new Rails app from my local development machine to a server I have that has a few Rails apps already on, bundle installs everything correctly and rake tasks run fine, however when I try and start the app it fails with that message.
I've:
Trashed my Gemfile.lock and reran bundle install
Done gem install rake to get the latest version
Checked rake --version shows 10.0.4.
Checked bundle show rake shows 10.0.4
Restarted the server
Now I'm drawing a blank on what it could be. Using Phusion Passenger, rails 3.2.13, ruby 1.9.2p290. I use rvm for managing the environment, but I've not changed anything with it for a long, long while now. The other Rails apps are all working okay without any issues, with similar gem dependencies.
Self fixed, but not sure why. I ran bundle install rake by accident, forgetting it doesn't do what I think and it created a ./rake folder which solved the issue.
Perhaps your application is being run as the wrong user.

Resources