1

我在让这个嵌入式 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 ')'   
4

1 回答 1

2

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

img_paths: [
  {
    targetId: "#SVGNonStandardHeroShapeImg1",
    newPath: image_path('cover-photo.jpg')
  },
  {
    targetId: "#SVGNonStandardHeroShapeImg2",
    newPath: image_path('cover-photo.jpg')
  },
].to_json
于 2020-01-03T23:29:38.397 回答