【问题标题】:asset pipeline not working on production, tried everythign资产管道无法用于生产,尝试了一切
【发布时间】:2012-07-03 06:26:23
【问题描述】:

我在 heroku 上有一个 rails 3.2 应用程序。我已经尝试了我能找到的一切来尝试解决 javascripts 没有在生产模式下加载的事实。不过在开发中一切正常。此外,在部署到 heroku 时,预编译失败。但是,当我查看 prod 中的源代码时,我可以清楚地看到 javascripts 文件 (/assets/application-32fdbd115c5d59c7be2876c103063600.js) 正在加载并且有内容。

我已经尝试了所有我能想到和读过的设置。我不太确定该怎么做。这是我目前在 production.rb 中的设置:

config.cache_classes = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.serve_static_assets = false
  config.assets.compress = false
  config.assets.compile = true
  config.assets.digest = true

  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify

在 environment.rb 中:

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

# Initialize the rails application
App::Application.initialize!

应用程序.rb:

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

require "rails/all"

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module App
  class Application < Rails::Application
config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]

    # Enable escaping HTML in JSON.
    config.active_support.escape_html_entities_in_json = true
config.active_record.whitelist_attributes = true

    # Enable the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'

还有我的 gemfile:

gem 'rails', '3.2.6'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'pg'
gem 'mysql2'
gem 'thin'
gem 'activerecord-postgresql-adapter'
gem 'devise'
gem 'paperclip', '~> 3.0'
gem 'haml'
gem 'activeadmin'
gem "meta_search", '>= 1.1.0.pre'
gem 'aws-sdk', '~> 1.3.4'
gem 'acts_as_list'
gem 'stripe'
gem 'sass-rails', '~> 3.2.3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'coffee-rails', '~> 3.2.1'
  gem 'twitter-bootstrap-rails'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

group :development do
  gem 'debugger'
end

我所做的一切似乎都无法让它在我的本地机器或 heroku 上的生产模式下工作。但是,它在开发中运行良好。

【问题讨论】:

  • 你试过重启 Heroku 吗? heroku restart

标签: ruby-on-rails asset-pipeline


【解决方案1】:

我终于想出了如何解决这个问题,尽管我不知道它的修复效果如何。基本上,问题在于将 .js 文件压缩到 application.js 中会导致问题。在 production.rb 我把这个:

  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = false

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = true

  # Generate digests for assets URLs
  config.assets.digest = true

  config.assets.debug = true

这解决了这个问题。但是,现在它列出了所有资产文件,而不是 application.js。我不确定这是否存在任何长期问题。

【讨论】:

    【解决方案2】:

    在你的生产盒上你试过了吗:RAILS_ENV rake assets:precompile

    【讨论】:

    • 是的,我尝试了几次不同的方法......但没有运气
    • 您是否收到任何类型的错误消息,或者只是未能提供资产?
    • 我没有看到任何错误消息,但是在尝试加载资产时,我在本地计算机上收到了 402 响应。这很奇怪,因为我看到源代码中的 javascripts 被压缩到 application.js 文件中。在 heroku 上,我确实收到预编译失败的错误。同样,我在 .js 文件中看到了资产。很奇怪
    • 我只是想出了一些东西......在生产中,如果我这样做 ?debug_assets=1 它可以解决问题。这意味着问题的原因是所有内容都被放入单个 application.js 文件中。知道如何在不关闭压缩的情况下解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 2013-01-15
    • 2012-04-23
    • 2021-09-11
    • 2022-12-05
    相关资源
    最近更新 更多