Rails 5 Assets Not Loading in Production

10,053

At times you need to add both of these configurations in staging.rb or whichever environment you want the changes to reflect on.

config.assets.compile = true #set to false by default
config.assets.digest = true 
Share:
10,053
Nicholas Yang
Author by

Nicholas Yang

Interesting in programming languages and compilers

Updated on June 17, 2022

Comments

  • Nicholas Yang
    Nicholas Yang almost 2 years

    I recently updated a few packages in my Rails application and now my assets aren't being served. Instead, I get the following error:

    Failed to load resource: the server responded with a status of 404 (Not Found)
    

    My assets are precompiled:

    assets.rb

    # Be sure to restart your server when you modify this file.
    
    # Version of your assets, change this if you want to expire all your assets.
    Rails.application.config.assets.version = '1.0'
    
    # Add additional assets to the asset load path
    # Rails.application.config.assets.paths << Emoji.images_path
    
    # Precompile additional assets.
    # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
    # Rails.application.config.assets.precompile += %w( search.js )
    Rails.application.config.assets.precompile += %w(ckeditor/*)
    Rails.application.config.assets.precompile += %w(ckeditor/config.js)
    Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )
    

    application.rb

    require_relative 'boot'
    
    require 'rails/all'
    
    # Require the gems listed in Gemfile, including any gems
    # you've limited to :test, :development, or :production.
    Bundler.require(*Rails.groups)
    
    
    module DeployTest
      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.
        config.assets.precompile += Ckeditor.assets
        config.assets.precompile += %w( ckeditor/* )
        config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
        config.active_record.default_timezone = :local
        config.time_zone = 'Eastern Time (US & Canada)'
      end
    end
    

    Before telling me to turn on compiling my assets, please understand that this is a horrible idea. Thank you for any advice

    UPDATE: I got it to work by adding:

      config.assets.digest = true
    

    to my config/environments/staging.rb file. Weird how I didn't need it before

  • Kartikey Tanna
    Kartikey Tanna over 4 years
    Setting a compile option to true is not recommended in the production environment. stackoverflow.com/a/8827757/2422068
  • jakenberg
    jakenberg about 3 years
    This exposes a major vulnerability in your app. DO NOT DO THIS