5

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

techdreams
  • 5,371
  • 7
  • 42
  • 63
Nicholas Yang
  • 317
  • 2
  • 3
  • 15
  • 1
    What did you upgrade? Can you git bisect to find the package that caused the issue and post it here? – Anthony Mar 02 '17 at 01:42
  • Yeah, I mean I did a bundle upgrade and it basically updated a bunch of packages. You can look at the list here: https://github.com/NicholasLYang/blog/commit/0fe7ef4b4408eb4283bef9d7f74f9c13a09adf4d – Nicholas Yang Mar 02 '17 at 05:13
  • What URL is returning 404 and where did you find that URL? The assets are compiled into public/assets. Whatever URL you are trying to request, it will 404 if it's not in that folder. And are you running rake assets:precompile? – mahemoff Mar 02 '17 at 09:47
  • Just FYI, you don't ever want to `bundle update` everything in your Gemfile. You typically do 1 gem at a time and before you do that, you read the release notes so you know about any big changes you need to worry about. – Anthony Mar 02 '17 at 12:48
  • @Anthony Yeah, you're right. I messed up. @mahemoff Capistrano does precompile assets, but yes, I did run `rake assets:precompile` before deploying. – Nicholas Yang Mar 02 '17 at 21:28
  • @mahemoff the URL is: http://staging.horriblyunderqualified.com/assets/application.css – Nicholas Yang Mar 03 '17 at 05:35
  • I would check that public/assets/application.css exists. If not, then you are either not compiling assets correctly or you are looking for the wrong file. The latter is more likely because if you have `digest` option turned on (which you should, and is on by default in production), the path will *not* be application.css. It will be some random name like application-0f9j48f90490fj3409.css. Look in your public/assets folder. Generated HTML will link to the digest filename, so your missing application.css is expected and not an issue. Just use standard Rails tags to link to assets from HTML. – mahemoff Mar 03 '17 at 14:13
  • Okay, interesting. You're right, application.css doesn't exist, because the digest option is turned on. However, I am using a standard Rails tag to link assets: `<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>` – Nicholas Yang Mar 04 '17 at 04:50

1 Answers1

3

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 
techdreams
  • 5,371
  • 7
  • 42
  • 63