Asset was not declared to be precompiled in production (Rails 5.0.0.1)
Asked Answered
C

2

8

I am new to Rails 5.0.0.1

I followed the guidelines in http://guides.rubyonrails.org/asset_pipeline.html#controller-specific-assets:

You can also opt to include controller specific stylesheets and JavaScript files only in their respective controllers using the following:

<%= javascript_include_tag params[:controller] %> or <%= stylesheet_link_tag params[:controller] %>

When doing this, ensure you are not using the require_tree directive, as that will result in your assets being included more than once.

I generated some scaffolds (which also generated the assets -- .coffee and .scss)

When I do rails s and access one of the scaffold's main page, it generated an error:

Asset was not declared to be precompiled in production.

Add Rails.application.config.assets.precompile += %w( myscaffold.css ) to config/initializers/assets.rb and restart your server


I do as it said, and it worked, but is there any other way to simplify this process?

I wish that I can generate some other scaffolds or controllers with their assets without doing any more concern of adding new lines inside the config/initializers/assets.rb.

I will receive any other alternatives too.

Cislunar answered 3/12, 2016 at 10:18 Comment(0)
E
8

you just try this..

check your Gemfile gem 'sprockets-rails', '2.3.3'

And bundle install

Endor answered 3/12, 2016 at 10:28 Comment(5)
And it's working @karthick. I used version 3.2.0. May I ask why it should be version 2.3.3? And thanks a lotCislunar
i used that version on my last project..you update can also put version.that's not a problem..Endor
That did it. How the spambiscuit would someone supposed to be productive without using on the fly compile for dynamic asset links, I don't know...Always
What I think happened here is, that the newer sprocket-rails gem added the error message "Asset was not declared to be precompiled in production." to warn developers of an error that otherwise would happen in production. In most development workflows you won't run into this issue very often, as with sprockets / asset pipeline you tend to compile all styles / javascripts into one (or few) files.Shortterm
I recommend bundle update instead of bundle install.Blower
S
2

Navigate files

config/initializers/assets.rb

Then add

Rails.application.config.assets.precompile += %w( your_file_name.scss )

Restart server

Sidonie answered 22/7, 2021 at 4:55 Comment(2)
this didnt solve the issue for meAmmonify
This saved my life. Because for me dartsass-ruby is now handling the compilation of SCSS to CSS, we need to tell Sprocket to leave those files alone.Rhea

© 2022 - 2024 — McMap. All rights reserved.