Asset filtered out and will not be served: add `config.assets.precompile

﹥>﹥吖頭↗ 提交于 2019-11-27 01:19:17

问题


I just migrated my Application to rails 4.1.0 from 4.1.rc2. Started getting these errors for my JS files

ActionView::Template::Error: Asset filtered out and will not be served: add `config.assets.precompile += %w( my_js )` to `config/application.rb` and restart your server

回答1:


Responding to urging from Heroku, the Rails maintainers have merged sanity checks from the sprockets_better_errors gem into Rails 4.1. See https://github.com/rails/sprockets-rails/pull/84

The intent is to reveal asset pipeline errors that you would see in production when you run the app in development mode.

You probably are using a javascript_include_tag in an application layout instead of placing your JavaScript files in the app/assets/javascripts/ folder.

You could move your files to the app/assets/javascripts/ folder.

Alternatively, you'll need to update your config/application.rb file to include:

config.assets.precompile += %w( my_js )

Note that the filename should not include the .js file extension.




回答2:


If you have added something like stylesheet_link_tag params[:controller] to your application layout header, because you have separate JS applications on different controllers but with same basic layout, you will be quite annoyed with this. Quick way to get over this and continue work (but maybe not a final solution) with all assets precompiled is to add the following to the /config/initializers/assets.rb:

Rails.application.config.assets.precompile += [/.*\.js/,/.*\.css/]

Note that this is as it is even for .js.coffee and .css.scss, .js and .css are enough.




回答3:


Ideally, the solution by @zmilojko works but I also have active admin in this app.. hence after looking a little further I found the following solution:

Rails.application.config.assets.precompile += %w(*.svg *.eot *.woff *.ttf *.gif *.png *.ico)
Rails.application.config.assets.precompile << /\A(?!active_admin).*\.(js|css)\z/

Just add the lines above to: /config/initializers/assets.rb

The code above precompiles assets while skipping active admin files. so that they are not processed twice or out of turn leading to errors.




回答4:


Yes, reading the error on the browser, all I did was add the following line of code:

Rails.application.config.assets.precompile += %w( depot.css )

To the /config/initializers/assets.rb file and it worked.




回答5:


You may detele <%= javascript_include_tag 'xxx' %> from your erb, and in you assets/javascript/name_space/index.js.coffee , append this

#= require ./xxx


来源:https://stackoverflow.com/questions/22970573/asset-filtered-out-and-will-not-be-served-add-config-assets-precompile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!