Rails: Sprockets::Rails::Helper::AssetNotPrecompiled in development
Asked Answered
M

12

58

I am relatively new to Rails and I am trying to use the asset pipeline, SCSS, in the development environment. However, after I created the controller, the view, and the css.scss file, I encounter the following error:

Sprockets::Rails::Helper::AssetNotPrecompiled

Here is the error message:

Asset was not declared to be precompiled in production. Add Rails.application.config.assets.precompile += %w( public.css ) to config/initializers/assets.rb and restart your server

I read that in development, the assets get compiled on the fly and there is no need to pre-compile. Why is there a pre-compile error? Did Rails think that I am in production instead of development?

Edited on 1 March 2016 ------

I just realized that adding files onto the config/initializers/assets.rb works. But is this the right way to do it? I have to add all the css/js/jpg files manually in the assets.rb for it to work. I felt that this somehow violate the DRY principle.

Mobile answered 28/2, 2016 at 13:35 Comment(0)
S
8

Please check the file

config/environments/development.rb

config.assets.debug = true (if TRUE change to FALSE)

Seicento answered 29/2, 2016 at 7:4 Comment(6)
config.assets.debug is set to True.Mobile
try to change it to FALSESeicento
It worked. Thanks. So debug mode does not preprocess the assets, that's why i need to set it off? So the scss files require preprocessing to work?Mobile
Welcome! Yes!, since it will takes time when its required to debug in development environment. But in production mode it is set to true, since you need to precompile all assets for faster rendering and interpreting.Seicento
This didn't work for me I am still getting the error, I changed it from true to falseHumphries
Yeah, See Ameer's answer. It worked for me in Rails 5.2.5 https://mcmap.net/q/328752/-rails-sprockets-rails-helper-assetnotprecompiled-in-developmentInfluent
S
148

Neither of these worked for me (rails-5.0.7, sprockets-3.7.2, sprockets-rails-3.2.1):

config.assets.debug = false
config.assets.unknown_asset_fallback = true

But this did:

config.assets.check_precompiled_asset = false
Saturated answered 17/8, 2018 at 14:56 Comment(9)
literally, config.assets.check_precompiled_asset worked for me too. rails version 4.2.8Neron
Thank you so much, this also worked for me in Rails 6.0.2.1Basion
This helped in api only app's issue in Apitome document parsing.Gunter
This worked for me in rails 6.0.3.3 when Sprockets couldn't figure out myfile.js.erb was a thingPomeranian
Worked when rails 6 + spina failed out of the box (Oct 2020)Evangelical
Worked for me, but why? App suddenly started throwing these errors for images. Chucking this in development.rb got it working again.Nottage
only this worked for me Rails 5.2.6 (Oct 2021)Mccabe
It works. But doesn't this mean, that it will not verify/check precompiled_assets ? Is it a good thing to do?Forespeak
Thanks. I don't understand that, but Thanks! It worked for me.Inquietude
C
55

I had the same problem and fixed it by deleting all the cache files inside the tmp folder

rm -rf tmp/cache/assets
Calciferous answered 27/3, 2020 at 10:41 Comment(3)
This worked for me, thanks! (rails 5.2.2 / sprockets 4.0.2 / sprockets-rails 3.2.2)Doriadorian
Thanks! This is the actual solution.rm -rf tmp/cache/assets/Thompson
Worked for me with Rails 6.1Ozell
P
30
//= link_directory ../javascripts .js

Adding this to app/assets/config/manifest.js
Worked for me (working on rails 6)

Pertussis answered 5/10, 2020 at 6:7 Comment(2)
I had to add this two lines to make it work //= link administrate/application.css //= link administrate/application.js using Rails 6.Leboeuf
Worked for me with rails 6.1.3Calmative
S
19

For those who came here after upgrading from rails 4 to 5-6 - checkout manifest.js

app/assets/config/manifest.js

If there is no link to js - just add one:

//= link_directory ../javascripts .js
Salmi answered 27/9, 2020 at 3:7 Comment(0)
U
9

Changing assets.debug did not work for me in Rails v5.1.5. I had to use this option: config.assets.unknown_asset_fallback = true

Ultrasonics answered 15/3, 2018 at 12:39 Comment(0)
S
8

Please check the file

config/environments/development.rb

config.assets.debug = true (if TRUE change to FALSE)

Seicento answered 29/2, 2016 at 7:4 Comment(6)
config.assets.debug is set to True.Mobile
try to change it to FALSESeicento
It worked. Thanks. So debug mode does not preprocess the assets, that's why i need to set it off? So the scss files require preprocessing to work?Mobile
Welcome! Yes!, since it will takes time when its required to debug in development environment. But in production mode it is set to true, since you need to precompile all assets for faster rendering and interpreting.Seicento
This didn't work for me I am still getting the error, I changed it from true to falseHumphries
Yeah, See Ameer's answer. It worked for me in Rails 5.2.5 https://mcmap.net/q/328752/-rails-sprockets-rails-helper-assetnotprecompiled-in-developmentInfluent
B
6

you can initialize this scss file for the following line of code

Rails.application.config.assets.precompile += %w( applicationname.css )
Brent answered 27/8, 2019 at 5:38 Comment(1)
file in which above needs to be changed: config/initializers/assets.rbMccabe
G
4

Try rake tmp:clear

References - https://github.com/rails/sprockets-rails/issues/458

Grugru answered 17/10, 2022 at 14:19 Comment(1)
bingoding is right! ...that's the sound of reliefCampbellite
M
2

I needed to declare my assets in my manifest:

//= link_directory ../stylesheets .css

in app/assets/config/manifest.js

Millrace answered 28/10, 2021 at 6:55 Comment(0)
R
1

Just adding here in case it might help someone.

I had a specific JS files I wanted only in a specific ActiveAdmin page and it was under /app/assets/javascripts/admin/orders.

Initially, none of the solutions worked for me beside adding this to config/environments/development.rb:

config.assets.check_precompiled_asset = false

A coworker mentioned that this change is a bit fishy and I tend to agree. Like what are the implications this will have on our development? Will we see errors related to precompilation of assets only in staging / prod from now?

Since I had subdirectories including JS files under my /app/assets/javascripts folder, I tried changing

//= link_directory ../javascripts .js

to

//= link_tree ../javascripts

And it solved it for me. Without the config.assets.check_precompiled_asset = false in the development.rb file.

Ratty answered 3/3, 2022 at 19:37 Comment(0)
L
0

Add this to app/assets/config/manifest.js

//= link_directory ../javascripts .js
Leverrier answered 28/9, 2021 at 7:38 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Cyprinid
M
0

Do not forget to restart the app too if you pulled new code.

Makedamakefast answered 7/5, 2022 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.