56

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.

Flip
  • 6,233
  • 7
  • 46
  • 75
Sydney
  • 1,349
  • 1
  • 14
  • 20

12 Answers12

146

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
eben.english
  • 1,625
  • 1
  • 11
  • 9
52

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

rm -rf tmp/cache/assets
gersanco
  • 533
  • 4
  • 5
30
//= link_directory ../javascripts .js

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

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
Ameer Hamza
  • 321
  • 3
  • 5
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
Max Paprikas
  • 579
  • 6
  • 16
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

Arctodus
  • 5,743
  • 3
  • 32
  • 44
8

Please check the file

config/environments/development.rb

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

aldrien.h
  • 3,437
  • 2
  • 30
  • 52
6

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

Rails.application.config.assets.precompile += %w( applicationname.css )
muthupandi
  • 63
  • 1
  • 5
2

I needed to declare my assets in my manifest:

//= link_directory ../stylesheets .css

in app/assets/config/manifest.js

Dorian
  • 7,749
  • 4
  • 38
  • 57
2

Try rake tmp:clear

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

bingoding
  • 21
  • 2
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.

RawKnee
  • 324
  • 3
  • 11
0

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

//= link_directory ../javascripts .js
Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
  • 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](/help/how-to-answer). – Community Sep 28 '21 at 07:47
0

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

iCyborg
  • 4,699
  • 15
  • 53
  • 84