Skip to content

Instantly share code, notes, and snippets.

@supairish
Created August 10, 2015 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supairish/27215e50d9a914c56c5b to your computer and use it in GitHub Desktop.
Save supairish/27215e50d9a914c56c5b to your computer and use it in GitHub Desktop.
Hey!
Here is what I currently use:
Put this in your application helper:
def image_assets
image_asset_dir = Rails.root.join('app', 'assets', 'images')
assets = {}
%w(.jpg .png .jpeg .svg).each do |file_ext|
Dir.glob(File.join(image_asset_dir, "**", "*#{file_ext}")).each do |absolute_path|
file = absolute_path.sub(File.join(image_asset_dir, '/'), '')
assets[file] = asset_url(file)
end
end
assets
end
Add this to you /app/assets/javascripts/assets.js.erb:
<% environment.context_class.instance_eval { include ApplicationHelper } %>
window.assets = window.assets || {};
window.assets.images = <%= raw(image_assets.to_json) %>;
Create a helper:
return window.assets.images[params[0]];
then you can do something like this in your template:
<img src="{{asset-path 'login.png'}}">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment