Assign Ruby string value to Javascrip variable?

C

cz

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
 
L

Lorenzo Brito Morales

i did something like these

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

Edmond Kachale

[Note: parts of this message were removed to make it a legal post.]

2011/2/15 Lorenzo Brito Morales said:
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%>";

2011/2/15 cz <[email protected]>



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.




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.

---
Edmond
Software Developer | Baobab Health Trust (http://www.baobabhealth.org/) |
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.*
 
E

Eric Christopherson

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:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top