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.
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:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.