1

Hello I have the following examples, I can't figure out how to make it work in html.haml.

Example 1 ( working):

# In html.erb file
<% @my_array = ['1, '2'] %>

<script>
  window.running_cycler = new MyAwesomeClass({
    custom_data: <%= raw @my_array %>
  });
</script>

Example 2 ( not working )

# In html.haml file
- @my_array = ['1', '2']

:javascript
  window.running_cycler = new MyAwesomeClass({
    custom_data: "#{raw @my_array}" 
    # or 
    # custom_data: "#{@my_array}"
  })

This is the browser error it throws. enter image description here How can I make it work in html.haml file?? It seems like raw is not working at all. If I don't use 'raw' then the format it gets converted is:

"[&quot;1&quot;, &quot;8&quot;]"

enter image description here

Please help. Thank you!

1 Answer 1

2

You can use single quotes and raw:

- @my_array = ['1', '2']

:javascript
  window.running_cycler = { 'custom_data': '#{raw @my_array}' }
  console.log(JSON.parse(window.running_cycler.custom_data).length)
  // 2
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thanks for the answer. It works with JSON.parse('#{raw @my_array}'). However I'm confused that I have to explicitly call JSON.pase while,Coffeescript should do that.
Apparently, window.running_cycler = { custom_data: #{raw @my_array} } is working as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.