开发者

Escaping " in Rails and Javascript

开发者 https://www.devze.com 2023-03-10 08:03 出处:网络
I have an array of hashes that consists of my product data in @product_records. I can extract the name of the product using the map function like this:

I have an array of hashes that consists of my product data in @product_records. I can extract the name of the product using the map function like this:

<%= @product_records.map{|x|x["Name"]} %>

which renders exactly how I want it to the page like this:

["Product1","Product2",...,"Productn"]

I want to try and pass this into a javascript variable so that I can use it with JQuery autocomplete.

var data = <%= @product_records.map{|x|x["Name"]} %>

When I try and set it though the double quotes are escaping like this:

[&quot;Product1&quot;, &quot;Product2&quot;,...,&quot;Productn&quot;]

I have tried various things to try and get the quotes back (.to_json etc) but nothing seems to work. There probably a very simple answer to this but I can't find what 开发者_如何学运维it is.

Cheers for any help.


Use <%= raw your_variable %> :)


When you use that variable in javascript make sure, you have single quote to execute rails code in javascript.

'<%= raw @products.to_json %>'

thanks


In my case I had to use like this, for example: <%= j raw @products %>

0

精彩评论

暂无评论...
验证码 换一张
取 消