asset-pipeline - 为什么 Rails4 放弃了 Gemfile 中对 "assets"组的支持

标签 asset-pipeline ruby-on-rails-4

在 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/

相关文章:

ruby-on-rails - rspec-core 中的 find_failed_line 错误

javascript - 为什么我的 CSS 不能在我的 Rails 应用程序上运行?

ruby-on-rails - 使用 Activeadmin + Paperclip Rails 直接上传到 Amazon S3

dojo - 如何将dojo工具包与rails 3.1 Assets 管道和coffeescript一起使用?

heroku - 尝试了几个修复: Heroku/Rails 4 Assets Precompile Error

ruby-on-rails - rails 4 : How do I handle a submitted form where nothing was selected?

postgresql - 创建 : could not connect to database template1: FATAL: password authentication failed for user

mysql - 根据数据库中存在的值显示选择选项

ruby-on-rails - 如何在 Rails 3 模型中访问 image_path 路由助手?

javascript - 外部 gem 的 Assets 管道编码问题(UTF-8 与 ASCII-8BIT)