# RJS throwing 'rubycode' exception

**URL:** <https://www.ruby-forum.com/t/rjs-throwing-rubycode-exception/84846>\
**Category:** Rails\
**Created:** [February 8, 2007, 2:03am UTC](https://www.ruby-forum.com/t/rjs-throwing-rubycode-exception/84846 "2007-02-08T02:03:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Frukn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@Mark\_Frukn](https://www.ruby-forum.com/u/Mark_Frukn)\
**Post date:** [February 8, 2007, 2:03am UTC](https://www.ruby-forum.com/t/rjs-throwing-rubycode-exception/84846/1 "2007-02-08T02:03:05Z")

</div>

All,

In an RJS template, how could I pass ruby variables values to the  
javascript without getting the RJS Exception: rubycode? This is what I’m  
doing (as an example) and is throwing the exception:

# index.rjs

names = [“ruby”, “ajax”]  
page \<\< “alert(#{names})”

You think that wouldn’t work because its an array, right? Ok, so if I  
convert the array to string:

# index.rjs

names = [“ruby”, “ajax”]  
page \<\< “alert(#{names.to\_s})”

still not working.

Also, it is possible to assign a JS variable’s value to a ruby variable?  
Like this pseudo code:

# index.rjs

page \<\< “js\_var = document.getElementById(‘my\_var’).value”  
ruby\_var = js\_var

Thanks a lot.

– Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Frukn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@Mark\_Frukn](https://www.ruby-forum.com/u/Mark_Frukn)\
**Post date:** [February 8, 2007, 6:23am UTC](https://www.ruby-forum.com/t/rjs-throwing-rubycode-exception/84846/2 "2007-02-08T06:23:23Z")

</div>

> names = [“ruby”, “ajax”]  
> page \<\< “alert(#{names.to\_s})”

I think this should do it:

names = [“ruby”, “ajax”]  
page \<\< “alert(’#{names.to\_s}’)”

(note the extra quotes in there for the javascript string.)
