RoR newbie: Regarding rails setup and rubygems - ruby-on-rails

I'm beginning to learn Ruby on Rails, I installed the latest ubuntu release on a VMWare machine and started the ruby on rails setup process using the mini-guide in this online book (which was recommended in stack overflow more than once). I'm wondering:
As part of my environment setup process I installed RubyGems and used it to install Rails (which is, if I understand correctly - a gem itself) the first time.
Now, when creating a new project using rails new project_name I later on edit the Gemfile and specify "rails '{version}'", which, from what I understand, installs the Rails gem in the context of the project (after using bundle install)...
Why would I need both of them? I'm kind of confused and would be more than happy if someone could shed some light over this...

Yes, this is a bit confusing.
When you run rails new project_name, you are using whatever rails executable is available in your shell to start a new rails project. To see what version this is, run rails —version. This does not "install the rails gem" — it just generates the tree of files to get you started on a new rails project.
You'll then specify in your Gemfile which version of rails your project will use.
So, be sure to have the latest version of rails available to your shell when you generate your new project, so that you get the newest version of the files it generates.

Related

How to collaborate remotely in rails with different Ruby versions?

Is there a way to generate a rails project with a 'minimum' Ruby version? Perhaps to exclude the version altogether?
Situation: I am working on an introductory rails project with a remote partner (paired programming). We have a small difference in ruby version (2.5.1 and 2.5.8).
Set-up:
$ rails _5.2.3_ new my_project_name -G --database=postgresql
We then update the gemfile to allow 'at least' version 2.5.1:
ruby '~> 2.5.1'
Each time we pass the project it requires that we edit the /.ruby-version file to 2.5.1 and 2.5.8 respectively.
This /.ruby-version change is what I am trying to solve more permanently.
I am new to programming!
There are ways you could force this to happen (go down the rabbit hole of .gitignoreing ruby version, etc)....but by far, the best way is to be developing on the same ruby version.

Updating Ruby 2.2.3 to Current

I received a ruby on rails application that was written three years ago
(Rails 4.2.1)
I need to update everything to current version of ruby, rails, rvm, bundler, rbenv, gems, etc...
Please help on process to achieving the update and the app to run not only locally but live.
To check what your ruby in now
$Ruby -v
Update it to ruby version 2.3.7
$rvm list known
$rvm install 2.3.7
$rvm use 2.3.7 --default
$gem install bundler
Let me know if does that help!
Hey dude upgrate from a very old to newest versions requires a lot of patient and skills. I found an article that might help you and also helped me before. I hope it can help you. Let me know if you have any more doubts
Upgrade ruby on rails project
Start a new Rails app with the latest Ruby, and use Test Driven Development to port the old app over, line by line.
Write a test that fails, copy a line out of the old app, pass the test, integrate & deploy, and repeat until all the features are installed - with tests.
TDD is where you write a test that fails, run it and make sure it fails for a correct, predictable reason, and only then add the production code that passes the test. Only integrate if all the tests pass. Learn more about TDD at its original web page: wiki.c2.com/?TestDrivenDevelopment . Learn to TDD in Rails by reading its flagship book, /Agile Web Development with Rails/.

Downgrading from Rails 3.2.6 to 3.0.11

I am facing issue when I am trying to work with rails 3.0.11. I initially worked with rails 3.2.6. Build a prototype application in Rails 3.2.6, Ruby 1.9.2 and gem 1.8.7. But then found, server on which we need to host is a shared server which can only support Rails 3.0.11, Ruby 1.8.7 and gem 1.7.2.
Hence I want to keep my dev environment exactly same before I ran into further issues. As I found there was some discrepancy in routes.rb of both the versions.
Steps I followed:
Uninstalled rails
Uninstalled ruby
Installed ruby from here.
This installed ruby version 1.8.7-p370 and gem 1.8.7
After this I installed rails by specifying version as 3.0.11
But still when I run command rails -v, it throws an error to me saying 'missing gem rails 3.2.6. Run bundle install to resolve the issue'
When I ran bundle install, it again completely installed rails 3.2.6 and problem remained unresolved.
Please suggest the clean way how shall I start my development on rails 3.0.11 without running into any issues?? Am I need to delete some registry or mapping in my system through which it is detecting rails 3.2.6 and creating issues??
Its urgent as I need to complete my dev by 7th September.
As I am new to stackexchange, please guide is it appropriate to ask question on the same related thread or shall I open new discussion for my problem?
#Gun - please note in response to one of your comments posted above - you DO need to change your Gemfile - if you are running rails -v from withing your project's directory, it will read the Gemfile.
There is a script located at script/rails - this is what runs when you run the rails command from the project directory. NOTE that this rails script requires your config/application.rb file - which in turn reads the Gemfile and bundles the referenced gems. Thus is your Gemfile still references 3.2.6, and it is not installed, rails -v will not work
Glad to hear the problem is fixed :)
As for the asset pipeline - if you are using rails 3.2.6 (or were using it) then you may have been using the asset pipeline to serve your assets (javascript files, CSS files, images, etc). If you were, and you downgrade to anything pre rails version 3.1, then anything being served through the asset pipeline will no longer work.
Here is a pretty good description of the asset pipeline: http://guides.rubyonrails.org/asset_pipeline.html
You should check also remove your Gemfile.lock or manually 'bundle update rails' when you change your rails dependency.
I suggest to use rbenv or rvm to create a virtual environment/sandbox where you can install your preferred ruby versions (e.g. 1.8.7) and keep tracking of your dependencies separately. This would save you a lot of effort.
Keep in mind that rails 3.0 has no asset pipeline support (it has been included in rails 3.1)! This is a very important difference and if you depend on it you will have big issues to solve for the downgrade.
Thanks a lot everyone for help.
I believe it was weird issue. I had heroku on the system. I was initially deploying my app on the same and then changed to another hosting server.
After uninstalling the heroku, it works perfectly fine. Probably, that was interfering somewhere. Although, not sure but problem is resolved.
Thanks a lot again for a great great help!!!
Meanwhile, I would request alup to throw some more light on asset pipeline support. Wots that? Could you please explain more??
Need to delete C:\Users\.ror folder and C:\Users\.gemfile and C:\Users\.gemfile.lock from your desktop..

Rails 2.3.2 app and rails 3 app on the same machine

I upgraded to the latest Rails v3.1.3. And everything is fine, I'm able to run the 2.3 version of one website and the app for v3.1.3.
However I couldn't move it to a new machine, I installed all the required gems with
bundle install
and just cannot start Thin with the 2.3.2 version. I can only run the v3 apps. Thanks
Go read about RVM, and try to configure you machine according to the instructions given there.
As #Syed Aslam said the ideal way of doing is using RVM, but you may set bundler to your Rails 2.x project (because rails 3.x already have bundler..) and run both your projects in the same machine (actually currently I'm doing that).
Following is a tutorial how setting up bundle with Rails 2.x.

Managing many Ruby on Rails applications of different versions

I'm learning Ruby on Rails with the AWDR book and have had to be specific about which version of Rails and Ruby that I am running on my local machine. I have just discovered that I need to roll back from ruby 1.8.7 to ruby 1.8.6 here. I also needed to roll back Rails to support the scaffold method so I could start the tutorial easily.
My question is: When I start contracting, developing and deploying projects in the real world, how am I going to manage all these different versions?
It looks to me like Rail's low tolerance for legacy code negates its ease of use philosophy! But I'm sure I'll grow to appreciate RoR.
As for Rails, What you can do is freezing your version, for example:
Make sure to install the proper Rails version, suppose you want version 2.2.2 : gem install rails v=2.2.2
Freeze and pack Rails with the project itself : rake rails:freeze:edge RELEASE=2.2.2
Now you will find Rails packed inside the vendor folder of your project, so you don't have to install Rails on the deploying machine.
And for Ruby, I like Ruby Version Manager(RVM), the easiest way to manage Ruby versions.
RubyGems is Ruby's package manager. You can install as many versions of gems (packages) as you want. You can install the latest by running sudo gem install rails (at the moment it will install 2.3.5). If you need 2.2.2, specify that with the -v or --version option: sudo gem install rails --version 2.2.2. Rails also installs a binary (yes, I know it's not really a binary file), rails, which generates a project. Because you have several versions of the gem, you need to control which binary gets called. When you install the rails gem, RubyGems puts a file in it's bin/ dir, which is a "link" to the real rails binary. That is the one you "call" when you say rails on the command line. However, all of the rubygems "link" binaries accept a parameter of it's own, which is what version you want to use. You would use the 2.2.2 rails binary like this:
rails _2.2.2_ my_project
I think the default is to use the most recent version, so if you want to use the most recent version, do this:
rails myproject
However, I see that you use 2.2.2 to get access to the scaffold method. I would strongly suggest you not to use that method, there's a reason for removing it. The scaffold method hides code, and makes customization hard. Instead, use the scaffold generator:
./script/generate scaffold --help
Good luck on your future rails adventures!
The latest version of Agile Web is written for 2.2.2 I believe. For this basic app they walk you through I'm very certain it should work with 2.3.x
The answer to the question for how you keep up is that you update your apps as needed and read the api and Changleogs to find out what has changed and fix the stuff that upgrades break. A great way to help with this is having a good test suite with good test coverage.

Resources