Search code examples
ruby-on-railsstylesheetasset-pipeline

Rails Assets Not Compiled in Production


Here's the error:

Processing by LandingPageController#index as HTML
  Rendered landing_page/index.html.erb within layouts/application (2.1ms)
Completed 500 Internal Server Error in 49ms

ActionView::Template::Error (landing_page.css isn't precompiled):
    12:     <![endif]-->
    13: 
    14:         <%= stylesheet_link_tag "application", :media => "all" %>
    15:         <%= stylesheet_link_tag params[:controller] %>
    16: 
    17:   </head>
    18:   <body>
  app/views/layouts/application.html.erb:15:in `_app_views_layouts_application_html_erb__3002306950342527375_29178380'

I can see that it's looking for the landing_page.css file because of line 15. What I don't understand is what is what is the best way to have this asset precompiled for production. I have tried modifying /config/application.rb:

config.assets.precompile += ['landing_page.css']

This doesn't seem right to me. I'd have to do this for every single stylesheet which would be annoying.

I added code to the application.css manifest:

*= require landing_page

This doesn't seem to work. I get the first error when I do this and don't modify the application config file.

I'm stumped as to how you can include the line

<%= stylesheet_link_tag params[:controller] %>

in your layout and have your assets precompiled for production when you run

bundle exec rake assets:precompile

I feel like I'm missing some simple trick that automatically adds the auto-generated stylesheets and javascript files to the list of files to be precompiled when you run the rake task.


Solution

  • Adding require landing_page to application.css does not cause landing_page to be precompiled. It means that when application.css is precompiled, the contents of landing_page.css will be included in the output.

    If you are going to load them individually, ie

        <%= stylesheet_link_tag params[:controller] %>

    Then you will need to add them all to the list of things to precompile. You can use wildcards  in that list, so if these controller specific stylesheets were all in stylesheets/controllers then you could do

        config.assets.precompile += ["controllers/*.css"]

    Typically though people tend not to do this. While for ease of development things are often split up on a per controller basis, all of these are then required from application.css. Application.css is then the only stylesheet you call stylesheet_link_tag on