在 Rails 3 中,专门用于在 Assets 管道中生成 Assets 的 gem 被正确放置在 Gemfile 的 assets
组中:
...
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails'
gem 'coffee-rails'
gem 'uglifier'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
end
现在,根据(仍在进行中)upgrade documentation :
Rails 4.0 removed the assets group from Gemfile. You'd need to remove that line from your Gemfile when upgrading.
果然,使用 RC1 创建一个新项目会生成一个 Gemfile,其中默认包含任何组之外的与 Assets 相关的 gem:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0.rc1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0.rc1'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
...
这是否意味着这些 gem 现在将默认捆绑在生产版本中?如果是这样,为什么会改变主意呢? Rails 4 是否正在朝着生产中动态生成 Assets 的方向发展?
最佳答案
以前, Assets 组的存在是为了避免生产中意外的按需编译。由于 Rails 4 不再有这样的行为,因此删除 Assets 组是有意义的。
这在the commit中有更详细的解释。这改变了这一点。我摘录了一些实际答案的引文。
Some gems can be needed (in production) like coffee-rails if you are using coffee templates and the fact that now assets are not precompiled on demand in production anymore.
(not precompiled on demand in production) Means that if you have that gems in production environment in 3.2.x and forget to precompile, Rails will do exactly what it does in development, precompile the assets that was requested. This is not true anymore in Rails 4, so if you don't precompile the assets using the tasks you will get a 404 when the assets are requests.
关于asset-pipeline - 为什么 Rails4 放弃了 Gemfile 中对 "assets"组的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16406204/