简体   繁体   中英

Correct path from js to assets/images path in rails app

How to correctly write path to 'assets/images/' inside a js.erb file? I am doing:

 icon: createImage('assets/'+img), 

interestingly, this works in localhost, but not in heroku. After searching in stackoveflow I try this:

 icon: createImage('<%= asset_path(img) %>'), 

I guess this one should help me. But I cannot pass img to asset_path .

Note: I am doing it in assets/javascript/map/gmap.js.erb

EDIT: I have found in rails guides that the files in app/assets are never served directly in production. So now i know why it isn't working in production. http://guides.rubyonrails.org/asset_pipeline.html

You can try:

icon: createImage('<%= image_tag(img) %>')

Especially if you are running Rails 3.1+ and using the Asset Pipeline, you want to use Rails helpers to specify files, not hard coded file paths. This should look for the img file in /assets as described here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM