# Assign Ruby string value to Javascrip variable?

**URL:** <https://rubytalk.org/t/assign-ruby-string-value-to-javascrip-variable/61821>\
**Category:** ruby-talk\
**Created:** [15 February 2011 11:05 UTC](https://rubytalk.org/t/assign-ruby-string-value-to-javascrip-variable/61821 "2011-02-15T11:05:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cz1](https://avatars.discourse-cdn.com/v4/letter/c/edb3f5/32.png) [@cz1](https://rubytalk.org/u/cz1)\
**Post date:** [15 February 2011 11:05 UTC](https://rubytalk.org/t/assign-ruby-string-value-to-javascrip-variable/61821/1 "2011-02-15T11:05:25Z")

</div>

Hi:

Is it possible to assign a ruby string value to a javascript variable?  
I can display the ruby value on the browser. It looks fine, but when I  
try to pass the value to a javascript variable, an error ocurrs  
(unterminated string literal). The code snippet below is my attempt.

var str = '\<%= #{@str} %\>';

I also tried the Ajax approach and that didn't work either. I am knew  
to Ruby, Sinatra, and Haml. That being said, I am surprised at how  
difficult this stuff is. Java and pHP were much easier. Any help or  
hints would be greatly appreciated. Thanks in advance.

cz

---

<div class="post-metadata">

**Author:** ![Lorenzo\_Brito\_Morale](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@Lorenzo\_Brito\_Morale](https://rubytalk.org/u/Lorenzo_Brito_Morale)\
**Post date:** [15 February 2011 11:09 UTC](https://rubytalk.org/t/assign-ruby-string-value-to-javascrip-variable/61821/2 "2011-02-15T11:09:27Z")

</div>

yes you can, but where you are doing it, in the view.js.erb file?

> **···**
>
> On Tue, Feb 15, 2011 at 5:05 AM, cz \<raymanojin@gmail.com\> wrote:
> 
> > Hi:
> > 
> > Is it possible to assign a ruby string value to a javascript variable?  
> > I can display the ruby value on the browser. It looks fine, but when I  
> > try to pass the value to a javascript variable, an error ocurrs  
> > (unterminated string literal). The code snippet below is my attempt.
> > 
> > var str = '\<%= #{@str} %\>';
> > 
> > I also tried the Ajax approach and that didn't work either. I am knew  
> > to Ruby, Sinatra, and Haml. That being said, I am surprised at how  
> > difficult this stuff is. Java and pHP were much easier. Any help or  
> > hints would be greatly appreciated. Thanks in advance.
> > 
> > cz

---

<div class="post-metadata">

**Author:** ![Eric\_Christopherson](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/eric_christopherson/32/1849_2.png) [@Eric\_Christopherson](https://rubytalk.org/u/Eric_Christopherson)\
**Post date:** [15 February 2011 23:29 UTC](https://rubytalk.org/t/assign-ruby-string-value-to-javascrip-variable/61821/3 "2011-02-15T23:29:43Z")

</div>

> Hi:
> 
> Is it possible to assign a ruby string value to a javascript variable?  
> I can display the ruby value on the browser. It looks fine, but when I  
> try to pass the value to a javascript variable, an error ocurrs  
> (unterminated string literal). The code snippet below is my attempt.
> 
> var str = '\<%= #{@str} %\>';

I'm guessing the problem is that you're using string interpolation  
syntax (#{...}), but outside of a double-quoted string. (Even if you  
were to use double quotes in your JavaScript here, it wouldn't could,  
because the erb expression is still not in Ruby quotes.) Just use:

var str = '\<%= @str %\>';

> **···**
>
> On Tue, Feb 15, 2011 at 5:05 AM, cz \<raymanojin@gmail.com\> wrote:
> 
> > I also tried the Ajax approach and that didn't work either. I am knew  
> > to Ruby, Sinatra, and Haml. That being said, I am surprised at how  
> > difficult this stuff is. Java and pHP were much easier. Any help or  
> > hints would be greatly appreciated. Thanks in advance.
> > 
> > cz

---

<div class="post-metadata">

**Author:** ![Bala\_TS](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@Bala\_TS](https://rubytalk.org/u/Bala_TS)\
**Post date:** [12 October 2011 10:17 UTC](https://rubytalk.org/t/assign-ruby-string-value-to-javascrip-variable/61821/4 "2011-10-12T10:17:46Z")

</div>

Hi:

Is it possible to Assign Java script value to Ruby value?  
I can save value to db

'\<% #{@str} %\>' = str;

it is possible then help to me?

By  
bdeveloper01:)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Lorenzo\_Brito\_Morale](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@Lorenzo\_Brito\_Morale](https://rubytalk.org/u/Lorenzo_Brito_Morale)\
**Post date:** [15 February 2011 11:16 UTC](https://rubytalk.org/t/assign-ruby-string-value-to-javascrip-variable/61821/5 "2011-02-15T11:16:12Z")

</div>

i did something like these

\<span class='progressBar' id='element\<%= todo.id %\>'  
so the name id=elementWhatevercomefromdatabase

> **···**
>
> On Tue, Feb 15, 2011 at 5:09 AM, Lorenzo Brito Morales \<lorenzo.brito@gmail.com\> wrote:
> 
> > yes you can, but where you are doing it, in the view.js.erb file?
> > 
> > On Tue, Feb 15, 2011 at 5:05 AM, cz \<raymanojin@gmail.com\> wrote:
> > 
> > > Hi:
> > > 
> > > Is it possible to assign a ruby string value to a javascript variable?  
> > > I can display the ruby value on the browser. It looks fine, but when I  
> > > try to pass the value to a javascript variable, an error ocurrs  
> > > (unterminated string literal). The code snippet below is my attempt.
> > > 
> > > var str = '\<%= #{@str} %\>';
> > > 
> > > I also tried the Ajax approach and that didn't work either. I am knew  
> > > to Ruby, Sinatra, and Haml. That being said, I am surprised at how  
> > > difficult this stuff is. Java and pHP were much easier. Any help or  
> > > hints would be greatly appreciated. Thanks in advance.
> > > 
> > > cz

---

<div class="post-metadata">

**Author:** ![Edmond\_Kachale](https://avatars.discourse-cdn.com/v4/letter/e/85e7bf/32.png) [@Edmond\_Kachale](https://rubytalk.org/u/Edmond_Kachale)\
**Post date:** [15 February 2011 13:11 UTC](https://rubytalk.org/t/assign-ruby-string-value-to-javascrip-variable/61821/6 "2011-02-15T13:11:56Z")

</div>

> yes you can, but where you are doing it, in the view.js.erb file?

I expect this to work pretty will even in view.html.erb. As I write, I am  
able to do this without problems as follows:

var userName = "\<%= @user.name%\>";

Is it possible to assign a ruby string value to a javascript variable?

I can display the ruby value on the browser. It looks fine, but when I try

> to pass the value to a javascript variable, an error ocurrs (unterminated  
> string literal). The code snippet below is my attempt.

var str = '\<%= #{@str} %\>';

> 

The question might be what is firing the bug, is it JS or Ruby? I suspect  
the Ruby string that is being passed is incorrect.

> **···**
>
> 2011/2/15 Lorenzo Brito Morales \<lorenzo.brito@gmail.com\>  
> 2011/2/15 cz \<raymanojin@gmail.com\>
> 
> ---  
> Edmond  
> Software Developer | Baobab Health Trust ([http://www.baobabhealth.org/\](http://www.baobabhealth.org/%5C)) |  
> Malawi
> 
> Cell: +265 999 465 137 | +265 881 234 717\*  
> \*\*  
> An old dog does not hunt because of speed, but his endurance of the heart.\*

---

<div class="post-metadata">

**Author:** ![Dave\_Aronson1](https://avatars.discourse-cdn.com/v4/letter/d/8e7dd6/32.png) [@Dave\_Aronson1](https://rubytalk.org/u/Dave_Aronson1)\
**Post date:** [14 October 2011 02:15 UTC](https://rubytalk.org/t/assign-ruby-string-value-to-javascrip-variable/61821/7 "2011-10-14T02:15:42Z")

</div>

Not easily, assuming you're doing client-side JS. By the time the  
browser is interpreting the JS, the Ruby is all done and gone (having  
executed on the server, in order to create the page with the JS on  
it). The closest you can do is to use the JS to create custom links,  
AJAX requests, etc. that you can fire off, and thereby affect values  
on the server.

-Dave

> **···**
>
> On Wed, Oct 12, 2011 at 06:17, Bala TS \<bdeveloper01@yahoo.com\> wrote:
> 
> > Is it possible to Assign Java script value to Ruby value?
> 
> --  
> LOOKING FOR WORK, preferably Ruby on Rails, in NoVa/DC; see main web site.  
> Main Web Site: davearonson.com  
> Programming Blog: codosaur.us  
> Excellence Blog: dare2xl.com
