Assign Ruby string value to Javascrip variable?

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

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

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 M.

2011/2/15 Lorenzo Brito M. [email protected]

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.

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.


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.*

On Tue, Feb 15, 2011 at 5:05 AM, cz [email protected] 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’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 %>’;

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:)

On Wed, Oct 12, 2011 at 06:17, Bala TS [email protected] wrote:

Is it possible to Assign Java script value to Ruby value?

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