Store Javascript variable's value into Rails variable

Hi,

I have a javascript function that returns a value. Is it possible to
store this value into a rails variable?

If you really want to know the reason why I need to do such a thing,
look at http://www.ruby-forum.com/topic/76733. If it’s possible to store
the javascript value into the rails variablem then the problem is
solved.

Thanks!

Aditya R. wrote:

I have a javascript function that returns a value. Is it possible to
store this value into a rails variable?

This is something that can be faked, but fundamentally the problem is
that JavaScript is executed in the browser (on the client) and Ruby code
is executed in the server. So, the server takes an RHTML page, executes
all of the Ruby code to fill in the dynamic stuff on the page, and ships
the page off to the client. At that point the Javascript runs, but all
of the Ruby code is gone - it was already executed.

So… it’s really easy to throw ruby variables into javascript variables
because the ruby execution happens first:

var jsVariable = “<%=rubyvariable%>”

However, if you want a value from javascript to be accessible in Ruby,
the only real option you have is to send it in a form (or querystring)
back to the server - either via an AJAX request or a regular form
submission.

Hope it helps,
Tom

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs