Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't Rails find my assets?

When in production mode, rails can't seem to find any precompiled assets from the asset pipeline.

I'm using rails 3.2.0 and ruby 1.9.3 running inside RVM on CentOS. No additional web server is running in conjunction with this application. The application was only recently updated to use the asset pipeline, as it was originally a rails 3.0 app.

After running

rake assets:clean
rake assets:precompile

I see the hashed content in public/assets, as I would expect. The hashes at the end of the files match those I see in the page source.

Yet at runtime, here's what I see for every asset Rails tries to serve:

Started GET "/assets/application-892c6227e631daf9a8e041b1d4d002ec.css" for 75.149.58.169 at 2012-03-14 11:42:43 -0700

ActionController::RoutingError (No route matches [GET] "/assets/application-892c6227e631daf9a8e041b1d4d002ec.css"):

I'm not referring to the folder that each asset is housed in; all references to assets look like these:

//css:
.class {
  background: url(asset.png) no-repeat;
}

//erb:
<%= image_tag "asset.png" %>
<%= link_to "page", :class => "class" %>

Asset pipeline pertinent settings in production.rb:

config.serve_static_assets = false
config.assets.enabled = true
config.assets.compress = true
config.assets.debug = false
config.assets.compile = false
config.assets.digest = true

And lastly, asset settings from config/application.rb:

config.assets.enabled = true
config.assets.version = '1.0'

The user starting the rails server process has read, write and execute permissions on public/assets, so I don't think it's a permissions issue. Have I missed a configuration step?

Edit

I noticed that there are no errors stating that assets are not precompiled, so I tried to access a stylesheet from the web page by appending"/assets/application-892c6227e631daf9a8e041b1d4d002ec.css" to the end of the host path:

http://www.myapp.com"/assets/application-892c6227e631daf9a8e041b1d4d002ec.css"

This worked and the stylesheet opened.

like image 207
BrMcMullin Avatar asked Mar 14 '12 19:03

BrMcMullin


2 Answers

Further researching of this issue yielded this SO article:

application.css not being served as an asset

It seems

config.serve_static_assets = false

Is an incorrect setting as long as my Rails application is not running behind Apache or nginx

like image 150
BrMcMullin Avatar answered Nov 16 '22 22:11

BrMcMullin


I had this same problem, but I note that your stylesheet is pointing to the non-fingerprinted, non-cached version of the files. If you are using the asset pipeline, in order to take advantage of it, you need to use the helpers that point to the fingerprinted, cached version of the files. To do this, you'll need to either embed erb in your css file, or use sass.

Incorrect:

.class {
  background: url(asset.png) no-repeat;
}

Correct (uses sass):

.class
  background: image-url('asset.png') no-repeat

For more info, see here: http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

If you don't care about the performance issues, you can get away with using the non-cached versions until you upgrade to Rails 4 or Rails 3.2.16, because those versions introduce breaking changes that force you to use the asset pipeline (and its corresponding syntax). If you don't use the new syntax, the non-cached versions will not work at all on production.

like image 42
Aaron Gray Avatar answered Nov 16 '22 22:11

Aaron Gray