Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to Prevent ' from turning into "

I have the following line:

data: <%= @product_count.to_json %>

which renders:

data: [[&quot;name1&quot;,20],
      [&quot;name2&quot;,9],
      [&quot;name3&quot;,18],
      [&quot;name4&quot;,32],
      [&quot;name5&quot;,28],
      [&quot;name6&quot;,17],
      [&quot;name7&quot;,11]]

in the view. How do I change this so I get the actual quotes instead of the HTML.

The data comes from this line:

period_registration_product.each do |key, value|
      @product_count << [Product.find_by_id(key).name, value]
end

Update:

I tried: data: <%= JSON.parse(@product_count.to_json) %> and it failed.

like image 609
Noah Clark Avatar asked Dec 26 '22 19:12

Noah Clark


2 Answers

I ended up using: data: <%= @product_count.to_json.html_safe %>

I tried both h and raw and neither of those worked, however, html_safe did it.

Not sure why, but for future travelers I'm on rails version 3.2.7.

like image 120
Noah Clark Avatar answered Jan 08 '23 09:01

Noah Clark


try

data:    <%= raw @product_count.to_json %>

or

data:    <%= @product_count.to_json.html_safe %>
like image 38
Pritesh Jain Avatar answered Jan 08 '23 07:01

Pritesh Jain