Rails 3.1 serving images from vendor/assets/images

I am trying to put some external images (used by a jQuery plugin) to vendor/assets/images in my Rails 3.1 app. Problem is that when I try something like:

<%= image_tag "ui-bg_flat_75_ffffff_40x100.png" %>

I get an error:

No route matches [GET] "/assets/ui-bg_flat_75_ffffff_40x100.png"

I checked my Rails.application.config.assets.paths and it list these dirs:

..../app/assets/images
..../app/assets/javascripts
..../app/assets/stylesheets
..../vendor/assets/images
..../vendor/assets/stylesheets
..../.rvm/gems/ruby-1.9.2-p180@mygems/gems/jquery-rails-1.0.9/vendor/assets/javascripts

As you can see /vendor/assets/images is listed there. If I put my image to app/assets/images everything works.

I thought that new asset pipeline was supposed to go through all assets dirs and serve requested file wherever it finds it.

Does anyone knows what's the problem here?


Solution 1:

I had to restart my rails server after creating the vendor/assets/images directory. Before this, I was seeing the same error as you ("No route matches [GET]").

My guess is that the rails server does not check these directories if they did not exist when it was first started. When you open a rails console to diagnose the issue, you get a new instance of rails which knows about the directory, which only adds to the confusion.

Solution 2:

If you are using a jQuery UI Theme Roller theme then the problem might be that in the jquery-ui css file the images are referenced within a sub folder 'images'.

I.e. you either have to put your images in a folder './app/assets/images/images' or you have to edit the jquery-ui css file and remove the 'images/' folder prefix.

Solution 3:

The asset pipeline is described in this rails guide by Ryan Bigg (draft status at the moment).

http://ryanbigg.com/guides/asset_pipeline.html and http://ryanbigg.com/2011/06/sprocket-asset-tags-internals/ for the references.

According to this, your example should work.

Extract:

Assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.

app/assets is for assets that are owned by the application, such as custom images, javascript files or stylesheets.

lib/assets is for your own libraries’ code that doesn’t really fit into the scope of the application or those libraries which are shared across applications.

vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins.

Any subdirectory that exists within these three locations will be added to the search path for Sprockets (visible by calling Rails.application.config.assets.paths in a console). When an asset is requested, these paths will be looked through to see if they contain an asset matching the name specified. Once an asset has been found, it’s processed by Sprockets and then served up.

I have tested with an example in my app and the same syntax as yours works. Maybe you have a typo in the name of your asset.

For Martin: search path for Sprockets is visible by calling Rails.application.config.assets.paths in a console.

Solution 4:

Maybe you should create another folder in /assets/images. You make a name 'images' and then you just copy all jquery-ui image and paste on folder 'images' that you create before. Hopefully this will help you.