Search code examples
cssruby-on-railsrubymine

Not able to include external stylesheet in rails application


I have style.css file in my assets/stylesheets directory and in the html I'm using<%= stylesheet_link_tag "/stylesheets/style.css" %>. I tried inspect element to check it, and css file is not being included.


Solution

  • Your problem has the hallmarks of a Rails asset pipeline problem

    1. Do you have an application.css.erb with something like this? It's created by default and serves as the backbone for the CSS aspect of the asset pipeline in rails.
    /* ...
    *= require_self
    *= require_tree .
    */
    
    1. Using the asset pipeline means that a call like "/stylesheets/style.css" would never be valid in production and should be avoided in code.

    If you're seeing this problem in production, did you rake your assets to build the fingerprinted filenames?

    The raked filenames are mangled to something like: /assets/style-4dd5b109ee3439da54f5bdfd78a80473.css, so you can see why using the filename "style.css" will not work.

    # Rake assets for production
    $bundle exec rake assets:precompile RAILS_ENV=production
    

    Debug

    To me step 1 is making sure your pipeline is setup to serve CSS assets. Until that works, your CSS will be broken. I would ask you to first check on the application.css.erb, to make sure your app includes your CSS tree in the pipeline. Then run the production rake, this will tell you what is happening with your CSS assets. If you post the output to your question, then we could see which, if any CSS is being included in your app.

    Required reading for anyone doing Rails apps is the Asset Pipeline guide