Embedded ruby syntax issue

41 views Asked by At

I'm having an issue getting this embedded Ruby to work:

<%= 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" } %>

There's an issue with the two calls to

<%= image_path(\'cover-photo.jpg\') %>

The errors are:

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

There are 1 answers

0
Alex Wayne On BEST ANSWER

I'd highly recommend building this data as a pure ruby object, and then encoding it as JSON. Then you don't have to worry about all the <%= tags so much.

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