Aditya Rajgarhia
2006-Aug-11 23:24 UTC
[Rails] 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! -- Posted via http://www.ruby-forum.com/.
Tom Lianza
2006-Aug-12 18:33 UTC
[Rails] Re: Store Javascript variable''s value into Rails variable
Aditya Rajgarhia 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 -- Posted via http://www.ruby-forum.com/.