嵌入式 ruby 语法问题

Embedded ruby syntax issue

我在让这个嵌入式 Ruby 工作时遇到问题:

<%= image_tag "svg/illustrations/non-standard-hero-shape.svg", class: "js-svg-injector", data: {
   img_paths: '[
                {"targetId": "#SVGNonStandardHeroShapeImg1", "newPath": "<%= image_path(\'cover-photo.jpg\') %>"},
                {"targetId": "#SVGNonStandardHeroShapeImg2", "newPath": "<%= image_path(\'cover-photo.jpg\') %>"}
               ]',
   parent: "#SVGNonStandardHeroShape" } %>

的两次调用都存在问题
<%= image_path(\'cover-photo.jpg\') %>

错误是:

syntax error, unexpected tIDENTIFIER, expecting '}'   
syntax error, unexpected ']', expecting ')'   

我强烈建议将此数据构建为纯 ruby 对象,然后将其编码为 JSON。那么你就不用太担心所有的 <%= 标签了。

img_paths: [
  {
    targetId: "#SVGNonStandardHeroShapeImg1",
    newPath: image_path('cover-photo.jpg')
  },
  {
    targetId: "#SVGNonStandardHeroShapeImg2",
    newPath: image_path('cover-photo.jpg')
  },
].to_json