2

Asset filtered out and will not be served: add Rails.application.config.assets.precompile += %w( login.js ) to config/initializers/assets.rb and restart your server

I ge the above error when i try to run my application.

<% content_for :javascripts do %>
    <%= javascript_include_tag 'login' %>
<% end %>

I have placed all my js files in assets/javascripts, but still i get the above error.

lulalala
  • 17,572
  • 15
  • 110
  • 169
theJava
  • 14,620
  • 45
  • 131
  • 172
  • 1
    Static assets without any preprocessing (i.e. no CoffeeScript, SASS/SCSS, ERB, ...) can be put in `public`. That way you can circumvent the whole assets pipeline, which seems to provide more problems than advantages in your current situation. – Daniel Rikowski Aug 05 '14 at 10:21

3 Answers3

8

As an alternative solution, you can also comment out this line on development.rb:

config.assets.raise_runtime_errors

or set it to false

This will disable sprockets runtime errors (the "Asset filtered out and will not be served" yada yada error) in development.

Keep in mind that by doing this, you can mask production errors for assets: eg. some assets will be served in development but not in production. (see this)

Use it if you know what you're doing :)

hth

David
  • 1,161
  • 10
  • 17
7

You must include all js/css files that you include with #javascript_include_tag and #stylesheet_link_tag methods in Rails.application.config.assets.precompile array. Check out config/initializers/assets.rb (create if it does not exist). That's how it should look:

Rails.application.config.assets.precompile += %w( login.js )

Restart Rails server after editing.

By the way it is self-evident from error's text.

Check out Rails Assets Pipeline documentation for details: http://guides.rubyonrails.org/asset_pipeline.html .

Nikita Shilnikov
  • 1,204
  • 9
  • 7
  • 1
    Why should we have to do this, when it used to be automatic? I now have about 80 .css and .js files I have to add manually? Whose bright idea was this anyway? – Lonny Eachus Jul 12 '14 at 03:16
  • It's not hard to explain. You can use as many js/css files as you want but compile all of them into one application.css file. One style file is better than many because it reduces client-server requests count consequently speeds up page loading. Also it's common to have several root style files e.g. login.css and admin.css. But Rails does not know which of all your js/css need to be compiled as root because the only way to know it is to render all possible templates. The only thing Rails can do is detect that you are include file that has not been marked as precompiled in prod environment. – Nikita Shilnikov Jul 12 '14 at 08:04
  • And after all you can use regex in precompile array if you definitely need all your 80 files to be compiled and not merged with precompile directive a-la `//= require_tree ./eighty-css` into one file. – Nikita Shilnikov Jul 12 '14 at 08:07
  • I added link to docs in my answer. – Nikita Shilnikov Jul 12 '14 at 08:09
2

I am doing something similar, but I've added my additional assets in Production.rb

config.assets.precompile += ['landing.css', 'landing.js']
John
  • 634
  • 8
  • 29