Skip to content

Instantly share code, notes, and snippets.

@melvynhills
Created April 24, 2012 21:14
Show Gist options
  • Save melvynhills/2483876 to your computer and use it in GitHub Desktop.
Save melvynhills/2483876 to your computer and use it in GitHub Desktop.
Rails image asset paths available in JavaScript
# Full code at https://gist.github.com/1406349/fa7c357b86790c183f3ef81ad6870b78bb2de343
window.assets = {
<%
Dir.glob(Rails.root.join("app/assets/images/**/*.*")).map do |path|
img_name = path.gsub(Rails.root.join("app/assets/images/").to_s, "") %>
"<%= img_name %>": "<%= asset_path(img_name) %>",
<% end %>
}
# Then all images are available as such: window.assets['my_image.jpg']
@melvynhills
Copy link
Author

Doesn't work for me.

@jbescoyez
Copy link

You are right. My mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment