Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails -- asset pipeline - identifying resource by directory

Say in vendor/assets I have two subdirectories, /alpha and /beta, each with a file named temp.jpg. To my understanding, a GET request is made for /assets/temp.jpg, and I'm pretty sure the one from the alpha directory is served. But how can I distinguish between the two of them? I think it can be done with the asset_url helper but I'm not quite sure - if anyone can advise that would be great.

like image 735
Kvass Avatar asked Mar 19 '12 13:03

Kvass


1 Answers

From the manual:


You can view the search path by inspecting Rails.application.config.assets.paths in the Rails console.

Additional (fully qualified) paths can be added to the pipeline in config/application.rb. For example:

config.assets.paths << Rails.root.join("app", "assets", "flash")

Sprockets will also look through the paths specified in config.assets.paths which includes the standard application paths and any path added by Rails engines.

Images can also be organized into subdirectories if required, and they can be accessed by specifying the directory’s name in the tag:

<%= image_tag "icons/rails.png" %>

If you're using the asset pipeline, I'm not sure which of your images would be supplied by a link that doesn't specify the url, if any. If one is being supplied, it's going to have to do with the order in which Sprockets recursively reads those directories. If it reads them in alpha order, then the "beta" image is going to be served. Or else it's just going to be random, I'm not sure how Sprockets reads the directories.

You're going to be better off putting an explicit path in there. If you are doing some sort of a test, like if you want the beta images to appear, I'd recommend some sort of paramaterized approach so that you can pass in "alpha" or "beta" to your path.

like image 186
AKWF Avatar answered Nov 15 '22 13:11

AKWF