在Rails3中,专门用于在资产管道中生成资产的gem被正确地放置在gem文件的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从Gemfile中删除了assets组。在升级时,您需要从Gemfile中删除该行。
果然,使用RC1创建新项目会生成一个Gemfile,其中包含默认情况下包含在任何组之外的与资产相关的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是否正在走向生产中资产的动态生成?
发布于 2013-06-21 02:36:50
以前,资产组的存在是为了避免生产中意外的按需编译。由于Rails4不再像那样运行,因此删除资产组是有意义的。
在改变了这一点的the commit中对此进行了更详细的解释。我摘录了一些带有实际答案的引文。
如果您使用的是咖啡模板,那么可能需要一些gem(在生产中),比如can rails,以及现在生产中不再按需预编译资产的事实。
(没有在生产中按需预编译)意味着如果你在3.2.x的生产环境中有这个gem,并且忘记了预编译,Rails将完全按照它在开发中所做的那样做,预编译所请求的资产。在Rails4中就不再是这样了,所以如果您不使用任务预编译资产,那么当资产是requests时,您将得到404。
发布于 2013-05-07 21:49:50
Rails4试图强制您在部署之前预编译您的资产。你必须用来预编译你的资产
$ RAILS_ENV=production bundle exec rake assets:precompile
为什么?我在Guide中找到了这个:
默认情况下,Rails假定资源已经被预编译,并且将由您的web服务器作为静态资源。
(来源:http://edgeguides.rubyonrails.org/asset_pipeline.html#in-production)
但很多时候你不得不在生产中使用这些“资产”宝石……例如,如果在视图目录中使用js.coffee文件,那么Rails在生产模式下也需要咖啡编译器。
所以我猜,这个变化的原因是性能的提高...而且看起来也更简单。:)
https://stackoverflow.com/questions/16406204
复制相似问题