Ruby-on-rails rake 资产:预编译 RAILS_ENV=生产未按要求工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22778784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rake assets:precompile RAILS_ENV=production not working as required
提问by Green
I am trying to precompile assets using the command rake assets:precompile RAILS_ENV=production, but I always get the error below.
我正在尝试使用命令预编译资产rake assets:precompile RAILS_ENV=production,但我总是收到以下错误。
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/usr/bin/ruby /usr/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
rake aborted!
LoadError: cannot load such file -- uglifier
(in /home/cool_tech/cool_tech/app/assets/javascripts/application.js)
/usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:251:in `require'
/usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:251:in `block in require'
/usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:236:in `load_dependency'
/usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:251:in `require'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/compressors.rb:31:in `registered_js_compressor'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/bootstrap.rb:18:in `block in run'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/compressors.rb:80:in `call'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/compressors.rb:80:in `compressor'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/compressors.rb:74:in `compress'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/processing.rb:265:in `block in js_compressor='
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/processor.rb:29:in `call'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/processor.rb:29:in `evaluate'
/usr/lib/ruby/gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/context.rb:193:in `block in evaluate'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/context.rb:190:in `each'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/context.rb:190:in `evaluate'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/bundled_asset.rb:26:in `initialize'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:252:in `new'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:252:in `build_asset'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/index.rb:93:in `block in build_asset'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/caching.rb:19:in `cache_asset'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/index.rb:92:in `build_asset'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:169:in `find_asset'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/index.rb:60:in `find_asset'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/static_compiler.rb:19:in `block in compile'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:219:in `block in each_logical_path'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:206:in `block (2 levels) in each_file'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:196:in `each'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:196:in `each_entry'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:204:in `block in each_file'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:203:in `each'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:203:in `each_file'
/usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:217:in `each_logical_path'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/static_compiler.rb:18:in `compile'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:56:in `internal_precompile'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:70:in `block (3 levels) in <top (required)>'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `call'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `block in execute'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `each'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `execute'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:165:in `invoke'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:60:in `block (3 levels) in <top (required)>'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `call'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `block in execute'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `each'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `execute'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:165:in `invoke'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:150:in `invoke_task'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `each'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `block in top_level'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:115:in `run_with_threads'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:100:in `top_level'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:78:in `block in run'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:176:in `standard_exception_handling'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:75:in `run'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/bin/rake:33:in `<top (required)>'
/usr/bin/rake:23:in `load'
/usr/bin/rake:23:in `<main>'
Tasks: TOP => assets:precompile:primary
rake aborted!
Command failed with status (1): [/usr/bin/ruby /usr/bin/rake assets:precomp...]
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils.rb:54:in `block in create_shell_runner'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils.rb:45:in `call'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils.rb:45:in `sh'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils_ext.rb:37:in `sh'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils.rb:82:in `ruby'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils_ext.rb:37:in `ruby'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:12:in `ruby_rake_task'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:21:in `invoke_or_reboot_rake_task'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:29:in `block (2 levels) in <top (required)>'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `call'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `block in execute'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `each'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `execute'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:165:in `invoke'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:150:in `invoke_task'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `each'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `block in top_level'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:115:in `run_with_threads'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:100:in `top_level'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:78:in `block in run'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:176:in `standard_exception_handling'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:75:in `run'
/usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/bin/rake:33:in `<top (required)>'
/usr/bin/rake:23:in `load'
/usr/bin/rake:23:in `<main>'
Tasks: TOP => assets:precompile
Does anybody has any idea what could be wrong? Thanks.
有没有人知道可能有什么问题?谢谢。
回答by Rajesh Omanakuttan
Replace
代替
rake assets:precompile RAILS_ENV=production
with
和
rake assets:precompile(RAILS_ENV=production bundle exec rake assets:precompileis the exact rake task)
rake assets:precompile(RAILS_ENV=production bundle exec rake assets:precompile是确切的耙子任务)
Since precompilation is done in production mode only, no need to explicitly specify the environment.
由于预编译仅在生产模式下完成,因此无需明确指定环境。
Update:
更新:
Try adding the below line to your Gemfile:
尝试将以下行添加到您的 Gemfile 中:
group :assets do
gem 'therubyracer'
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
end
Then run bundle install.
然后运行bundle install。
Hope it will work :)
希望它会起作用:)
回答by Rajdeep Singh
Have you added this gem to your gemfile?
你有没有把这个 gem 添加到你的 gemfile 中?
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
move that gem out of assets group and then run bundle again, I hope that would help!
将该 gem 移出资产组,然后再次运行 bundle,我希望这会有所帮助!
回答by Richard Peck
To explain the problem, your error is as follows:
为了解释这个问题,你的错误如下:
LoadError: cannot load such file -- uglifier
(in /home/cool_tech/cool_tech/app/assets/javascripts/application.js)
This means somewhere in application.js, your app is referencing uglifier(probably in the manifestarea at the top of the file). To fix the issue, you either need to remove the reference to uglifier, or make sure the uglifierfile is present in your app, hence the answers you've been provided
这意味着在 中的某个地方application.js,您的应用正在引用uglifier(可能在manifest文件顶部的区域)。要解决此问题,您需要删除对 的引用uglifier,或确保该uglifier文件存在于您的应用程序中,因此已为您提供了答案
Fix
使固定
If you've had no luck with adding the gem to your GemFile, a quick fix would be to remove any reference to uglifierin your application.jsmanifest. This, of course, will be temporary, but will at least allow you to precompile your assets
如果你已经受够了加入宝石你的Gemfile没有运气,速战速决将删除任何引用uglifier您的application.js清单。当然,这将是暂时的,但至少可以让您预编译您的资产
回答by Green
I found out that my back-up project worked well if I precompile without bundle update. Maybe something went wrong with gem updated but I don't know which gem has an error.
我发现如果我不使用bundle update. 也许 gem 更新出了问题,但我不知道哪个 gem 有错误。
回答by Andrew WC Brown
I had this problem today. I fixed it being being explict about my require
我今天遇到了这个问题。我修复了它明确说明我的要求
gem 'uglifier', '>= 1.0.3', require: 'uglifier'
I had mine still in the assets group.
我的仍然在资产组。

