Rails 4 Heroku 资产:预编译失败
Posted
技术标签:
【中文标题】Rails 4 Heroku 资产:预编译失败【英文标题】:Rails 4 Heroku assets:precompile failure 【发布时间】:2014-01-19 15:56:31 【问题描述】:我有一个奇怪的情况,突然面临预编译失败,老实说,由于某种原因我看不到这个问题。
我在这里添加了一个包含日志和参考文件的要点 https://gist.github.com/malagodia/8506722
让我困惑的是,我阅读了 Heroku 关于数据库连接的建议,并明确确保 application.rb 包含
config.assets.initialize_on_precompile = false
这里是日志输出错误,完整日志就大意了
Running: rake assets:precompile
rake aborted!
undefined method `[]=' for nil:NilClass
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/config/initializers/database_connection.rb:7:in `block (2 levels) in <top (required)>'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:27:in `each'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/config/initializers/database_connection.rb:5:in `block in <top (required)>'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:36:in `call'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:44:in `each'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/application/finisher.rb:62:in `block in <module:Finisher>'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `instance_exec'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `run'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/initializable.rb:54:in `run_initializers'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/application.rb:215:in `initialize!'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/config/environment.rb:5:in `<top (required)>'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `block in require'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/application.rb:189:in `require_environment!'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/application.rb:250:in `block in run_tasks_blocks'
/tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/sprockets-rails-2.0.1/lib/sprockets/rails/task.rb:54:in `block (2 levels) in define'
Tasks: TOP => environment
(See full trace by running task with --trace) ! ! Precompiling assets failed. !
! Push rejected, failed to compile Ruby app
heroku 指出的附加痕迹 ~ $ rake assets:precompile --trace ** 调用资产:预编译(first_time) ** 调用资产:环境(first_time) ** 执行资产:环境 ** 调用环境 (first_time) ** 执行环境 ** 执行资产:预编译
【问题讨论】:
同样的问题 ... ``` NoMethodError: undefined method[]=' for nil:NilClass ... /tmp/.../config/initializers/database_connection.rb:7:in
block (2 levels) in 我最近从 Rails 4 升级到 4.1 时遇到了这个问题。
为了解决这个问题,我刚刚删除了文件database_connection.rb
。由于 Heroku 在 Concurrency and Database Connections in Ruby with ActiveRecord 上的文章,我得到了这个文件。
希望对你有帮助!
【讨论】:
【参考方案2】:我相信您的config
值是nil
,因为在您预编译资产时数据库(或环境中的任何其他内容)不可用,因此您应该尽量避免与数据库相关的任何事情在那个阶段。情况描述here。
他们描述的解决方案只是“不要那样做”:
“一旦你隔离了错误,你必须通过不依赖环境变量和预编译时存在的数据库来修复它。”
【讨论】:
感谢您的建议。我试图通过简单地关闭数据库来在本地复制它,但根本无法得到同样的问题。 我不认为这完全复制了这种情况,但无论如何,提供的链接描述了如何确认这是否是问题。无论哪种方式,Heroku 的指导都非常清楚,在预编译期间依赖环境是一个坏主意。 再次感谢,我确实运行了建议的方法进行复制,不幸的是它不会导致致命错误。奇怪的是 Heroku 关于管理并发的建议link 引入了这个问题,显然我在 unicorn 和 database_connection 之间的交互中遗漏了一些东西 感谢您的回答,这是正确的,尽管我对 Heroku 自己的建议为什么不起作用感到困惑 - 当我弄清楚时(总有原因)我会分享回来跨度> 作为后续,heroku 支持向我显示环境加载得太早,这导致了问题。不幸的是,我什至找不到更早的分支,这给了我不同的跟踪,很难调试。上面粘贴的跟踪【参考方案3】:我的情况是,我忘记更新 application.css 的扩展名以包含 .scss。所以,我猜,sprockets 试图编译一个不存在的 .scss 文件;因此 nil 错误。
我通过运行“RAILS_ENV=production bundle exec rake assets:precompile”在本地但在生产环境中编译我的资产(根据 Heroku 文档:https://devcenter.heroku.com/articles/rails-asset-pipeline)并查看最后几行输出来识别错误(输出比将完整的应用程序推送到 Heroku 更干净、更快捷):
NoMethodError: nil:NilClass 的未定义方法 `[]' (在 /other_stuff/my_app/app/assets/stylesheets/application.css 中)
我注意到了 .css 扩展名并添加了 .scss 来修复错误。
【讨论】:
以上是关于Rails 4 Heroku 资产:预编译失败的主要内容,如果未能解决你的问题,请参考以下文章
Rails 4 - 生产资产:预编译导致 ActiveRecord::NoDatabaseError: FATAL: database does not exist