Acquire Asset Path from JavaScript

前端 未结 6 982
梦如初夏
梦如初夏 2021-02-18 16:17

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

6条回答
  •  清酒与你
    2021-02-18 16:35

    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");
    

提交回复
热议问题