Adinda
Adinda

Reputation: 1101

Migrating up to Rails 3.1.3, all images inaccessible in production

All my images are inaccessible on production env after upgraded to rails 3.1.3 from 3.0.3. Even if I manually go to http://localhost:3000/rails.png and put the image file to public/ I get Routing Error. Same thing if I moved it to public/images when accessing to localhost:3000/images/rails.png.

I'm not sure it's misconfigured asset settings. I tried following similar thread in Stack overflow with no luck. Here are my Gemfile and my config files.

The 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

Upvotes: 0

Views: 683

Answers (2)

Kikoo
Kikoo

Reputation: 11

Open your to production.rb and then,

config.serve_static_assets = true

Upvotes: 1

Richard Hulse
Richard Hulse

Reputation: 10493

You are probably getting a routing error because when the pipeline is enabled it is assumed that all assets are in the pipeline. You will have image tag helpers somewhere that cannot find the required asset.

With the setting you have (compile = true) all requests for assets are passed to Sprockets.

All assets appear in the path /assets by default. You should also change these two lines:

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

to this:

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

And run the precompile task before testing.

You should also remove the x_sendfile_header option. Production servers need to be specially set up to use this, and some servers don't support it (giving you blank images).

You can check your settings against those shown in the last section of the asset pipeline guide.

Once you have the settings above fixed, delete the sprockets cache in tmp/cache/assets/ and restart your server.

Upvotes: 3

Related Questions