迁移到Rails 3.1.3,生产中无法访问所有图像

时间:2011-12-17 10:20:42

标签: ruby-on-rails-3 routes asset-pipeline

从3.0.3升级到rails 3.1.3后,我的所有图像都无法在生产环境中访问。即使我手动转到http://localhost:3000/rails.png并将图像文件放到public/,我也会收到路由错误。如果我在访问public/images时将其移至localhost:3000/images/rails.png,则相同。

我不确定资产配置是否配置错误。我尝试在Stack溢出中跟随类似的线程没有运气。这是我的Gemfile和我的配置文件。

Gemfile

source 'http://rubygems.org'
gem "rails", "3.1.3"
gem "pg", :require => "pg"
gem "devise", "~> 1.5.2"
gem "cancan"
gem "hoptoad_notifier", '2.4.11'
gem "friendly_id", "~> 3.1"
gem "will_paginate", "~> 3.0.2"
gem "haml", "~> 3.1.3"
gem "haml-rails"
gem "yard"
gem "bluecloth"
gem "simple_form", '~> 1.3.1'
gem "paperclip"
gem "jquery-rails"
gem 'twitter-bootstrap-rails'
gem 'thin'

group :assets do
  gem 'sass-rails', " ~> 3.1.5"
  gem 'coffee-rails', " ~> 3.1.0"
  gem 'uglifier'
end

group :development, :test do
  gem "rspec-rails", "~> 2.7.0"
  # gem 'remarkable_activerecord', '~> 4.0.0.alpha4'
  gem "maksar-remarkable_activerecord", "~> 4.0.0.alpha6"
  gem "faker"
  gem "evergreen", :require => "evergreen/rails"
  gem 'pry'
end

group :development do
  gem "rails3-generators"
  gem "hpricot"
  gem "ruby_parser"
end

group :test, :cucumber do
  gem "factory_girl_rails"
  gem "database_cleaner"
  gem "timecop"
  gem "pickle"
  gem "spork", "~> 0.9.0.rc2"
end


group :cucumber do
  gem "cucumber"
  gem "cucumber-rails"
  gem "capybara", "~> 0.4.0"
  gem "launchy"
end

group :test do
  gem "mocha"
  gem "autotest"
  gem "autotest-rails"
end

application.rb中

module MyTutorial
  class Application < Rails::Application
    # ... other config...
    # 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'
    config.assets.initialize_on_precompile = false
  end
end

production.rb

MyTutorial::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # The production environment is meant for finished, "live" apps.
  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

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

  # Don't fallback to assets pipeline
  config.assets.compile = true

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

  # Specify the default JavaScript compressor
  config.assets.js_compressor  = :uglifier

  # Specifies the header that your server uses for sending files
  config.action_dispatch.x_sendfile_header = "X-Sendfile"
end

2 个答案:

答案 0 :(得分:3)

您可能收到路由错误,因为在启用管道时,假定所有资产都在管道中。您将在某处无法找到所需资产的图像标记助手。

使用您具有的设置(compile = true),所有资产请求都将传递给Sprockets。

默认情况下,所有资源都显示在路径/assets中。您还应该更改这两行:

config.assets.compile = true
config.assets.digest = false

到此:

config.assets.compile = false
config.assets.digest = true

在测试之前运行预编译任务。

您还应该删除x_sendfile_header选项。生产服务器需要专门设置才能使用它,有些服务器不支持它(给你空白图像)。

您可以根据资产管道指南的last section中显示的设置检查您的设置。

修复上述设置后,删除tmp/cache/assets/中的链轮缓存并重新启动服务器。

答案 1 :(得分:1)

打开您的production.rb,然后

config.serve_static_assets = true