Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping " in Rails and Javascript

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.

like image 946
GrahamJRoy Avatar asked Jun 03 '11 09:06

GrahamJRoy


People also ask

What is the synonym of escaping?

Some common synonyms of escape are avoid, elude, eschew, evade, and shun.

What's a word for escaping reality?

escapism; escape from reality. escapism. escape from reality.

What word is escape?

verb (used without object), es·caped, es·cap·ing. to slip or get away, as from confinement or restraint; gain or regain liberty: to escape from jail. to slip away from pursuit or peril; avoid capture, punishment, or any threatened evil.

What does escaping time mean?

What Does Escape Time Mean? Escape time refers to the length of time that is available to (and necessary for) an individual to escape from a specific hazardous situation.


2 Answers

Use <%= raw your_variable %> :)

like image 187
Svilen Avatar answered Sep 29 '22 09:09

Svilen


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

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

thanks

like image 25
Rameshwar Vyevhare Avatar answered Sep 29 '22 08:09

Rameshwar Vyevhare