Rails with backbone-rails: asset helpers (image_path) in EJS files

后端 未结 5 516
无人共我
无人共我 2020-12-13 11:24

I have a Rails 3.1 app that uses the codebrew/backbone-rails. In a .jst.ejs template, I would like to include an image, like so:



        
5条回答
  •  攒了一身酷
    2020-12-13 11:45

    I can see two ways. Neither are great.

    When you say <%%= variable %> then this is rendered by ERB as <%= variable %>, so you could double percent escape everything but the asset_tags and that would survive the trip through one ERB pass on the way to EJS.

    If you find that too gross...

    How about making a different javascript file, with an ERB extension, that defines your asset paths? And then use the asset pipeline to require that.

    So say assets.js.erb defines something like:

    MyAssets = {
      'foo': <%= image_path("foo.png") %>,
      ...
    }
    

    And then require this somewhere near the top of your manifest. And then reference the globals however that works in EJS.

提交回复
热议问题