Forum: Ruby on Rails RJS throwing 'rubycode' exception

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
3e3c9bcd31e3758aec353debced2ca64?d=identicon&s=25 Mark Frukn (Guest)
on 2007-02-08 02:03

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:

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:

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:

page << "js_var = document.getElementById('my_var').value"
ruby_var = js_var

Thanks a lot.

-- Mark
Bd62c7b1443e9161f0d7d8a757f3f836?d=identicon&s=25 Harlan Crystal (Guest)
on 2007-02-08 06:23
(Received via mailing list)
> 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.)
This topic is locked and can not be replied to.