# Values broken converting from rails into javascript

**URL:** <https://www.ruby-forum.com/t/values-broken-converting-from-rails-into-javascript/126454>\
**Category:** Rails\
**Created:** [January 18, 2008, 11:30am UTC](https://www.ruby-forum.com/t/values-broken-converting-from-rails-into-javascript/126454 "2008-01-18T11:30:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 18, 2008, 11:30am UTC](https://www.ruby-forum.com/t/values-broken-converting-from-rails-into-javascript/126454/1 "2008-01-18T11:30:46Z")

</div>

I have a section of javascript which i’m trying to use to dynamically  
generate the options for a mult-select box, when another box’s contents  
are changed. Basically i have a method that returns an array of arrays,  
where each array is of the form

[name, number]

I then iterate over the arrays, and substitute in the values out of the  
array -

\<%options\_for\_select(SubjectFamilyMember.options\_for\_select([“3”])).each\_with\_index  
do |option\_tag, i| -%\>  
$(“subject\_family\_members[]”).options[\<%= i+1.to\_i %\>]=new Option(’\<%=  
option\_tag[0] %\>’, \<%= option\_tag[1].to\_i %\>, false, false);  
\<%end %\>

When i run this in the console (escaping the quotes), it seems to  
produce the right results -

$(“subject\_family\_members[]”).options[1]=new Option(“All”, 0, false,  
false);  
$(“subject\_family\_members[]”).options[2]=new Option(“Violin”, 13, false,  
false);  
$(“subject\_family\_members[]”).options[3]=new Option(“Viola”, 14, false,  
false);  
$(“subject\_family\_members[]”).options[4]=new Option(“Cello”, 15, false,  
false);  
$(“subject\_family\_members[]”).options[5]=new Option(“Double Bass”, 16,  
false, false);

However, when it’s run on the page, all the strings come out as “60” and  
all the numbers come out as “111” - i see this javascript when i look at  
the page source -

$(“subject\_family\_members[]”).options[1]=new Option(‘60’, 111, false,  
false);  
$(“subject\_family\_members[]”).options[2]=new Option(‘60’, 111, false,  
false);  
$(“subject\_family\_members[]”).options[3]=new Option(‘60’, 111, false,  
false);  
$(“subject\_family\_members[]”).options[4]=new Option(‘60’, 111, false,  
false);  
$(“subject\_family\_members[]”).options[5]=new Option(‘60’, 111, false,  
false);

So, the ‘i’ out of my ‘each\_with\_index’ is coming through fine, but the  
array elements are being broken somehow. Does anyone know what might be  
going wrong here?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 18, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/values-broken-converting-from-rails-into-javascript/126454/2 "2008-01-18T12:56:05Z")

</div>

After a lot of mucking about, i discovered that it seems to be some  
problem with ‘each\_with\_index’ - if i do

\<% for option\_tag in SubjectFamilyMember.options\_for\_select([“3”]) -%\>

instead, and do the indexing with a local javascript variable then it  
seems to work fine, at least in terms of producing javascript using  
rhtml tags.

I’d still really like to know why the arrays were all coming out as  
[“60”, 111] when i used each\_with\_index though, in case anyone has any  
ideas about that…
