Rails Gemfile:在开发中使用一行,在生产中使用另一行
我们在生产中使用Heroku运行Unicorn,但在本地机器上使用Webrick进行开发。 我们无法在本地计算机上安装Unicorn。
是否有可能让Rails仅在生产中加载Unicorn gem? 现在,我们的解决方案是在本地运行应用程序时注释掉Unicorn gem,并在推送到Heroku时取消注释gem。
我们在Rails 3.2.12上。
的Gemfile:
source 'http://rubygems.org' gem 'rails', '3.2.12' gem 'jquery-rails' # # ========================================================================================= # # #========================================================================================= gem 'mongo' gem 'mongo_mapper' gem 'plucky' gem 'bson_ext' gem 'bson' gem 'newrelic_rpm' gem 'rpm_contrib' # Gems used only for assets and not required # in production environments by default. group :assets do gem 'sass-rails', '~> 3.2.3' gem 'coffee-rails', '~> 3.2.1' gem 'uglifier', '>= 1.0.3' end
谢谢!
是否有可能让Rails仅在生产中加载Unicorn gem? 现在,我们的解决方案是在本地运行应用程序时注释掉Unicorn gem,并在推送到Heroku时取消注释gem。
是的,可以通过在Gemfile
使用组来Gemfile
。 仅针对production
的unicorn
gem更新您的Gemfile
,如下所示:
# Gemfile group :production do gem 'unicorn' end
由于WEBrick
是rails应用程序的默认Web服务器,因此您无需为development
组指定任何内容。
Gemfile
更新后运行bundle install
仍将安装生产gem。 这绝对是一件好事,因为您希望确保您计划在生产中使用的gem在项目的开发阶段与您的应用程序一起正常工作。
要跳过production
组gems的安装:
bundle install --without production
关于--without production
选项需要注意的一点是,随后对bundle install
和bundle update
调用也将跳过安装和更新生产gem。 要禁用此function,您需要从app_root/.bundle/config
删除行BUNDLE_WITHOUT: production
:
# app_root/.bundle/config BUNDLE_WITHOUT: production