预编译资产失败的 Rails

Posted

技术标签:

【中文标题】预编译资产失败的 Rails【英文标题】:Precompile Assets Failed Rails 【发布时间】:2013-11-11 23:56:45 【问题描述】:

我在 Heroku 上部署 Rails 4.0.1 但是当我尝试预编译时出现以下错误:

$ rake assets:precompile
rake aborted!
uninitialized constant SampleApp
c:/Sites/sampleapp/config/initializers/secret_token.rb:27:in `<top (required)>'
c:/Sites/sampleapp/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => environment
(See full trace by running task with --trace)

这是我的环境.rb:

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
Sampleapp::Application.initialize!

secret_token 中发现问题的那一行:

SampleApp::Application.config.secret_key_base = secure_token

这是我的 application.rb:

require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)

module Sampleapp
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.rb,yml').to_s]
    # config.i18n.default_locale = :de
  end
end

【问题讨论】:

Heroku 将为您预编译您的资产。您无需先在本地执行此操作。 另一方面,我看到您的应用名称有多种情况:有时我看到 SampleApp,有时我看到 Sampleapp。如果你让这些一致,它将有助于你的情况。 谢谢!我只需要更改 secret_token(SampleApp 到 Sampleapp)。但是我不明白为什么它也发现环境错误...... 【参考方案1】:

根据application.rb,您的项目使用Sampleapp 作为其名称。因此,请确保您的所有核心文件在您的选项配置中使用相同的名称——包括大写

# config/initializers/secret_token.rb
Sampleapp::Application.config.secret_key_base = secure_token

【讨论】:

以上是关于预编译资产失败的 Rails的主要内容,如果未能解决你的问题,请参考以下文章

Rails 4 Heroku 资产:预编译失败

ruby on rails升级资产预编译失败

远程:!预编译资产失败。在heroku上(Rails 6和语义UI)

资产:使用 uglify 预编译失败

宝石更新后:测试失败,“资产未声明为在生产中预编译”

推送到 heroku 时出现“预编译资产失败”错误