在Heroku Cedar上为Rails 3.2应用程序未执行资产预编译。

21 浏览
0 Comments

在Heroku Cedar上为Rails 3.2应用程序未执行资产预编译。

我本周开始开发一个新的应用程序,它使用最新的Rails 3.2.2版本和资源管道。对于资源,我希望在将应用程序推送到Heroku时进行编译(而不是手动编译和存储已编译的资源在仓库中)。文档建议这应该自动发生。\n问题是当我运行git push heroku master时,似乎根本没有运行rake assets:precompile任务。资源从未被编译。这是我得到的输出:\n

-----> Heroku收到推送
----->检测到Ruby/Rails应用程序
----->使用Bundler版本1.1.rc.7安装依赖项
       运行:bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
       使用rake(0.9.2.2)
       .......
       .......
     您的bundle已经完成!它被安装到./vendor/bundle中
       清理bundler缓存。
----->编写config/database.yml以从DATABASE_URL读取
----->Rails插件注入
       注入rails_log_stdout
       注入rails3_serve_static_assets
----->发现进程类型
       Procfile声明类型->web
       Ruby/Rails的默认类型->console,rake,worker
----->编译的slug大小为61.7MB
----->启动...完成,v30
       [appname]部署到Heroku

\n我在我的application.rb中启用了资源管道\n

require File.expand_path('../boot', __FILE__)
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
Bundler.require *Rails.groups(:assets) if defined?(Bundler)
module Appname
  class Application < Rails::Application
    # ...
    # 启用资源管道
    config.assets.enabled = true
    # 在预编译资源时不启动应用程序
    config.assets.initialize_on_precompile = false
  end
end

\n有谁知道可能是什么原因引起的问题?如果需要,我很乐意提供更多的代码。如果我运行heroku info,它显示我正在Cedar堆栈上运行。\n为了避免在生产环境中收到500错误,我不得不打开config.assets.compile = true,尽管这会在运行时编译资源(这是我不想要的)。

0
0 Comments

问题的原因是Heroku Cedar上的Rails 3.2应用程序没有运行Assets precompile。这可能是因为在production.rb中设置了config.assets.compile = false,导致assets没有被编译。将额外的assets添加到precompile列表中也没有帮助。

解决方法是在每次更改assets时运行bundle exec rake assets:precompile命令,并将其推送到master分支。然而,这需要手动进行操作,而且不符合Heroku应该自动编译assets的文档说明。

值得注意的是,升级应用程序从Rails 3.1到3.2后,assets:precompile阶段不再在slug编译期间执行。这可能也是问题的原因之一。

可能的解决方法是手动运行bundle exec rake assets:precompile命令,并将编译后的assets添加到git仓库中,以确保在Heroku上正确运行。

0
0 Comments

在Heroku Cedar上运行Rails 3.2应用程序时,无法运行Assets precompile的问题可能是由于Rakefile中的错误导致的。在这种情况下,precompile步骤将被完全跳过。对我来说,问题是由于我引用了一个只在测试和开发环境中加载的依赖项,而不是生产环境,所以Rakefile失败了。

我使用-Lao的建议解决了这个问题。

heroku run rake -T --app staging-hawkmo
Running `rake -T` attached to terminal... up, run.1
rake aborted!
cannot load such file -- jasmine-headless-webkit

一旦我看到问题就是在Rakefile的require中,只需要将其包装在一个环境测试中即可解决。

0
0 Comments

问题的原因是Rakefile在生产环境中没有加载assets:precompile任务。Rakefile加载了cucumber和rspec gem的任务,但这些gem没有在生产环境中打包。因此,在生产环境中,assets:precompile任务不可用,因此Heroku没有尝试运行它。

解决方法是将测试任务包装在一个环境检查条件中。具体做法是,在Rakefile中添加以下代码:

if %(development test).include?(Rails.env)
  require 'rspec/core'
  require 'rspec/core/rake_task'
end

另外,还有一位用户发现了这个问题并找到了另一种解决方法。他在文件的内容周围添加了一个begin/rescue块。具体做法如下:

begin
  # 原始代码内容
rescue
  # 异常处理代码
end

这样做可以解决这个问题。感谢这个发现!作者之前也遇到了同样的问题,他添加了一个依赖于rspec的新的rake任务,导致了这个问题的出现。通过以上两种方法,作者和其他用户都成功解决了这个问题。

0