Why does stylesheet_link_tag not link to /assets in production?

13,315

Solution 1

When the helper generates this code:

/stylesheets/application.css

This is because the pipeline is NOT enabled in whatever mode you are in. There are several possible reasons.

  • You have the pipeline enabled in the wrong config file. (The pipeline should be enabled in the application.rb config file)

  • You have it enabled in application.rb and disabled somewhere else.

  • You have accidentally commented out a railtie somewhere

See this question for details.

If this is an upgraded app, check all the config options in the last section of the pipeline guide to make sure that you have them all correctly set.

Solution 2

I know this is for rails 3.1 but a very similar error can happen to users of rails 4 so for the sake of completeness and to help future googlers. Most probable cause is you didn't added that asset to a precompile directive in production.rb, it looks like this:

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
config.assets.precompile += %w(  )

As the OP said, application.css wasn't being added and the symptom is the url begins with /stylesheets or /javascripts and not assets. So the solution is to

config.assets.precompile += %w( your_assets.css your_assets.js )

Solution 3

WARNING: Setting config.assets.compile = true in production can make your app vulnerable to Rails Asset Pipeline Directory Traversal Vulnerability (CVE-2018-3760) .

I would suggest you to enable config.assets.compile = false(by default) to true in production.rd and see the assets are served as in developemnt. If they are correctly served then you should check your application.css to see if you are including other stylesheets in the directory properly like having css files

/*
*= require scaffold
*= require pagination
*= require_self
*= require_tree.
*/

where scaffold and pagination are css files. or mention them under config.assets.precompile flag as below.

config.assets.precompile += %w(pagination.css scaffold.css )

I assume the reason being precompile works (application.js, application.css, and all non-JS/CSS are already added) and any additonal assets should be added to config.assets.precompile flag.

Share:
13,315

Related videos on Youtube

siro neko
Author by

siro neko

Ruby/Rails programmer, designer and photographer from Groningen, The Netherlands.

Updated on June 04, 2022

Comments

  • siro neko
    siro neko almost 2 years

    I just did the first deploy for a new Rails 3.1 application, but the assets don't seem to work correctly. I'm precompiling everything upon deploy, and it turns up in public/assets like expected. However, the output of for example stylesheet_link_tag "application" in my layout has a href pointing to /stylesheets/application.css. This obviously does not work.

    The strange thing is that in development mode everything seems to be fine, it links to /assets/application.css like expected.

    I've compared my config/application.rb and config/environments/production.rb files with another app we have on the asset pipeline, and the relevant settings seem to be the same.

    Where should I look?

    • naren
      naren
      Are you sure that the application.css in production merges has all the css you used in development. Open the file and check for missing style in the application.css
  • naren
    naren over 12 years
    In production the assets would be served from publci/assets directory so we need to precompile the assets to move from app/assets(etc) to publc/assets
  • siro neko
    siro neko over 12 years
    I've turned compilation on, but that didn't make a difference. I've also turned on config.assets.debug = true, but that didn't change anything either. The public/assets/manifest.yml file exists, and contains an entry application.css: application-642da156f9df61facf341073150aa860.css, and that file exists.
  • naren
    naren over 12 years
    You have said that in development mode everything works fine and turning config.assets.compile = true should been the same. Can you post the gist of your heirarchy in the app/assets directory
  • siro neko
    siro neko over 12 years
    Thanks for pointing me in the right direction. I was not require'ing all of Rails (mongodb-only app), and was missing a require "sprockets/railtie". Still not sure why this didn't matter in development.
  • Fabrizio Regini
    Fabrizio Regini about 10 years
    please can you tell me more on how you fixed this? I'm having the same issue after an upgrade from 3.x.
  • redtree
    redtree almost 10 years
    In my case on Rails 4.0, the issue was that I was giving the helper the wrong path. Giving it the correct one used /assets instead of /stylesheets