Rake Assets:Precompile 将 rails 应用程序推送到 heroku 时出错

Rake Assets:Precompile error pushing rails app to heroku

首先我要说我现在非常讨厌 heroku。

好的,开始吧。我正在将一个应用程序推送到 heroku,并收到 "precompiling assets failed" 错误。我已经尝试了大约 100 种方法来修复它,但没有任何效果。我试过设置

    config.assets.initialize_on_precompile = false

在我的 application.rb 文件中,不起作用。我试过在本地预编译、提交和推送,以及隐藏本地预编译资产。我也尝试过在不同的环境中推动。

这是我的错误日志,事情开始发生的地方:

> Preparing app for Rails asset pipeline
   Running: rake assets:precompile
   rake aborted!
   SyntaxError: (erb):82: syntax error, unexpected ':', expecting ')'
   (erb):85: unterminated string meets end of file
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/mongoid-4.0.0/lib/mongoid/config/environment.rb:40:in `load_yaml'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/mongoid-4.0.0/lib/mongoid/config.rb:83:in `load!'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/mongoid-4.0.0/lib/mongoid.rb:99:in `load!'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/mongoid-4.0.0/lib/mongoid/railtie.rb:75:in `block in <class:Railtie>'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/railties-4.1.6/lib/rails/initializable.rb:30:in `instance_exec'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/railties-4.1.6/lib/rails/initializable.rb:30:in `run'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/railties-4.1.6/lib/rails/initializable.rb:55:in `block in run_initializers'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/railties-4.1.6/lib/rails/initializable.rb:54:in `run_initializers'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/railties-4.1.6/lib/rails/application.rb:300:in `initialize!'
   /tmp/build_1843337b33065f83ff572061af54f827/config/environment.rb:5:in `<top (required)>'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/railties-4.1.6/lib/rails/application.rb:276:in `require_environment!'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/railties-4.1.6/lib/rails/application.rb:389:in `block in run_tasks_blocks'
   /tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/sprockets-rails-2.2.4/lib/sprockets/rails/task.rb:64:in `block (2 levels) in define'
   Tasks: TOP => environment

我在顶部附近看到 "syntax error",但我不知道要查看哪个文件。 谁能帮帮我?

这是我的 mongoid.yml 文件的结尾,从第 78 行到第 90 行

78    staging:
79      sessions:
80        default:
81          database: <dbname>
82          hosts: 
83           - <host>:<port>
84          username: <username> 
85          password: <password> 
86        options:
87    production:
88      sessions:
89        default:
90          uri: <%= ENV['MONGOHQ_URL'] %>

堆栈跟踪的前几行指出了问题所在:

Running: rake assets:precompile
rake aborted!
SyntaxError: (erb):82: syntax error, unexpected ':', expecting ')'
(erb):85: unterminated string meets end of file
/tmp/build_1843337b33065f83ff572061af54f827/vendor/bundle/ruby/2.1.0/gems/mongoid-4.0.0/lib/mongoid/config/environment.rb:40:in `load_yaml'

Mongoid 尝试加载其 YAML 配置时发生错误,mongoid.yml

找到问题

这和Heroku没有关系stacktrace中提到的mongoid.yml这一行指向staging部分,你应该可以通过 运行:

在本地重现错误
bundle exec rake assets:precompile RAILS_ENV=staging

顺便说一句,你还是运行Rails3吗?因为 according to Heroku:

In Rails 4.x this option [initialize_on_precompile] has been removed and is no longer needed.

解决方案

我的猜测是您需要引用包含冒号的字符串。应该是:

78    staging:
79      sessions:
80        default:
81          database: <dbname>
82          hosts: 
83           - '<host>:<port>'
84          username: <username> 
85          password: <password> 
86        options:
87    production:
88      sessions:
89        default:
90          uri: <%= ENV['MONGOHQ_URL'] %>

不是- <host>:<port>(注意上面的单引号)。