22

I need to display images on an HTML5 canvas that are in the Rails asset pipeline, but I need to know the path for the asset from JavaScript. I'm using js-routes for other parts of the application, but it doesn't appear to provide a way to get the path to something in the asset pipeline.

What's the correct way to obtain the path to a Rails asset (e.g., an image) from JavaScript?

senfo
  • 28,488
  • 15
  • 76
  • 106

6 Answers6

25

In the Rails Asset Pipeline guide, they give an example of coding assets in your stylesheets by preprocessing the stylesheets with ERB. You can use the same technique with JavaScript, assuming you tack an .erb to the end of the filename:

var someAssetPath = "<%= asset_path('some_image.png') %>";
BaronVonBraun
  • 4,285
  • 23
  • 23
  • Thank you for the quick response....I thought about doing this, but there could be many different assets I'd want the path to, so this could get cumbersome and turn into a maintenance nightmare as assets are added or removed. I also considered creating my own action method to return the URL for the provided asset name, but I was hoping to find a better alternative. – senfo Nov 15 '12 at 16:08
  • 4
    I personally think this is a terrible suggestion, even though it is viable. Rails is not intended to be a JavaScript precompiler. – arxpoetica Mar 04 '14 at 16:44
  • 1
    @ArxPoetica, then why did they embrace CoffeeScript that compiles to JavaScript? – Mischa Nov 16 '15 at 12:05
9

Checkout the js_assets(Javascript helper in rails projects) gem. I think it is precisely what you need.

From the documentation:

Get the path to the template app/assets/javascripts/rubrics/views/index.html in javascript: var path = asset_path('rubrics/views/index.html')

Pikachu
  • 774
  • 13
  • 15
7

Why not add a data attribute for the path inside an element in your .erb file and then retrieve that with JQuery?

inside some_template.html.erb

<%= content_tag(:div, "", id: 'some-id', data:{path_to_asset: asset_path("some_image.png")}) %>

then in some_javascript.js

var assetPath = $("#some-id").data("pathToAsset");
ryan2johnson9
  • 724
  • 6
  • 21
  • 1
    i kind of like this suggestion. Instead of appending .erb to your js file. – kev Apr 12 '15 at 15:34
  • 2
    I like this too, because if you need to use a variable derived from javascript in your image path, you can't use asset path with erb injection. – Jesse Farmer Nov 20 '15 at 06:33
6

For those using HAML you can do:

:javascript
   var assetPath = "#{asset_path('some_image.jpg')}";
Michael Yagudaev
  • 6,049
  • 3
  • 48
  • 53
4

I came across the same issue in Rails 4.1 and used referencing rails assets in coffeescript for images. No additional libraries needed.

Artur Hebda
  • 49
  • 1
  • 2
0

In my case, I wanted to get the stylesheet path and the hash that rails generates for cache busting made it impossible to hardcode.

What ended up working quite well for me is to assign an ID to the main stylesheet link element in the html (layout) and then use javascript to extract the href. If you want the base asset path, perhaps create a generic element with the data you need as an attribute.

Rendered HTML

<link rel="stylesheet" href="mypath/main.css" type="text/css" id="main-css">

JS

$("#main-css").attr("href"); // "mypath/main.css"
karns
  • 5,391
  • 8
  • 35
  • 57