简体   繁体   中英

Rails 5: rails s vs. bundle exec rails s

I'm starting a project on Rails 5 for the first time and I was curious why running 'rails s' when I was on Rails 4 worked fine, but now that I'm on Rails 5 I need to preface it with 'bundle exec' in order to run the command properly.

Below is my Gemfile. Again, everything works normally if I preface all my rails commands with. I'm just curious if anyone else experiencing this or if someone can give me some insight as to why this is happening?

 source 'https://rubygems.org'
 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
 # Use postgresql as the database for Active Record
 gem 'pg', '~> 0.18'
 # Use Puma as the app server
 gem 'puma', '~> 3.0'
 # Use SCSS for stylesheets
 gem 'sass-rails', '~> 5.0'
 # Use Uglifier as compressor for JavaScript assets
 gem 'uglifier', '>= 1.3.0'
 # Use CoffeeScript for .coffee assets and views
 gem 'coffee-rails', '~> 4.2'
 # See https://github.com/rails/execjs#readme for more supported runtimes
 # gem 'therubyracer', platforms: :ruby

 # Use jquery as the JavaScript library
 gem 'jquery-rails'
 # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
 gem 'turbolinks', '~> 5'
 # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
 gem 'jbuilder', '~> 2.5'
 # Use Redis adapter to run Action Cable in production
 # gem 'redis', '~> 3.0'
 # Use ActiveModel has_secure_password
 # gem 'bcrypt', '~> 3.1.7'

 gem 'bootstrap', '~> 4.0.0.alpha3'
 source 'https://rails-assets.org' do
 gem 'rails-assets-tether', '>= 1.1.0'
 end
 gem "paperclip", "~> 5.0.0"
 gem "browser"
 # Use Capistrano for deployment
 gem 'capistrano-rails', group: :development

 group :development, :test do
 # Call 'byebug' anywhere in the code to stop execution and get a debugger console
 gem 'byebug', platform: :mri
 end

 group :development do
 # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
 gem 'web-console'
 gem 'listen', '~> 3.0.5'
   # Spring speeds up development by keeping your application running in the  background. Read more: https://github.com/rails/spring
   gem 'spring'
   gem 'spring-watcher-listen', '~> 2.0.0'
 end

 # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
 gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

It sounds like the rails command you have installed globally is rails 4 and it's the reason "it doesn't run properly" like you say. When you run bundle exec then it uses the rails commands from the current Gemfile , since you have rails 5 in your Gemfile then it works properly.

I'd recommend using either RVM or rbenv , I personally like RVM since it switches automatically to the gemset of the ruby version specified in my Gemfile.

Example, when I cd into the directory of my project I get a message like:

RVM used your Gemfile for selecting Ruby, it is all fine - Heroku does that too.

Using bundle exec guarantees that the program is run with the environment specified in the gemfile, which hopefully means it is the environment that the creators of the program want it to be run in, which hopefully means it should run correctly no matter what weird setup you have on your computer.

Reference: Use bundle exec rake or just rake?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM