Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails asset pipeline not working in production environment?

I have recently upgraded an app from Rails 3.0 to 3.1. I have followed any instructions I could find for enabling the asset pipeline but it always fails when in the production environment:

<%= javascript_include_tag "application" %>

gives me

<script src="/javascripts/application.js" type="text/javascript"></script>

which is missing a digest and I get the following error:

cache: [GET /javascripts/application.js] miss
Started GET "/javascripts/application.js" for 127.0.0.1 at 2011-10-03 23:31:36 +0100
ActionController::RoutingError (No route matches [GET] "/javascripts/application.js"):

I've tried variations of these settings in application.rb:

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

#require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"

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 Blog
  class Application < Rails::Application
    config.autoload_paths += %W(#{config.root}/lib)
    config.encoding = "utf-8"
    config.filter_parameters += [:password]
    config.assets.enabled = true
    config.assets.version = '1.0'
  end
end

and full production.rb (minus some comments)

Blog::Application.configure do
  config.cache_classes = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.serve_static_assets = false
  config.assets.compress = true
  config.assets.compile = false
  config.assets.digest = true
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
end

I have ran the rake assets:precompile task.

Am I missing any obvious steps?

Edit: Some additional details:

My assets are in app/assets folder. app/assets/images, app/assets/javascripts, app/assets/stylesheets, etc.

I see my files generated in my public/assets directory with names and digests.

app/assets/javascripts/application.js does indeed compile to something like public/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js They end up in the public directory.

like image 379
tpower Avatar asked Oct 03 '11 22:10

tpower


People also ask

How to enable asset pipeline in Rails?

The asset pipeline is implemented by the sprockets-rails gem, and is enabled by default. You can disable it while creating a new application by passing the --skip-asset-pipeline option. The sassc-rails gem is automatically used for CSS compression if included in the Gemfile and no config. assets.

What does rake assets precompile do?

rake assets:precompile. We use rake assets:precompile to precompile our assets before pushing code to production. This command precompiles assets and places them under the public/assets directory in our Rails application.

What is assets in Ruby on Rails?

The lib/assets (or sometimes vendor/assets ) is where you supposed to place js/css/images related to your front-end design and provided by third party libs. Say if you want to pull in some css or a js framework, that were you should place it.


2 Answers

Sprockets is not getting loaded.

In an effort to remove active record in a previous version of rails (a la this question Remove ActiveRecord in Rails 3 (beta)) the require 'rails/all' was replaced by

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"

What was missing here was sprockets/railtie

like image 138
tpower Avatar answered Dec 24 '22 11:12

tpower


See the Upgrading to Rails 3.1 Railscast

Make sure your assets are in app/assets folder. app/assets/images, app/assets/javascripts, app/assets/stylesheets, etc.

Execute rake assets:precompile

You should see files generated in your app/public/assets directory with names and digests if enabled.

app/assets/javascripts/application.js would compile to /assets/application-6ec417a53cb2bdb949966a153a61e7b1.js

If the asset is named similar to above with a digest, Production.rb should have the following config:

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

If you look at the web page source you should see something similar to the following:

<script src="/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js" type="text/javascript"></script>

Try to manually load the file by going to http://example.com//assets/application-6ec417a53cb2bdb949966a153a61e7b1.js

The file should load, if not try checking permissions and further logs.

like image 35
Joey Avatar answered Dec 24 '22 10:12

Joey