# Store Javascript variable's value into Rails variable

**URL:** <https://www.ruby-forum.com/t/store-javascript-variables-value-into-rails-variable/102168>\
**Category:** Rails\
**Created:** [August 12, 2006, 1:23am UTC](https://www.ruby-forum.com/t/store-javascript-variables-value-into-rails-variable/102168 "2006-08-12T01:23:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 12, 2006, 1:23am UTC](https://www.ruby-forum.com/t/store-javascript-variables-value-into-rails-variable/102168/1 "2006-08-12T01:23:53Z")

</div>

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 [Radio Button events - Rails - Ruby-Forum](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!

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 12, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/store-javascript-variables-value-into-rails-variable/102168/2 "2006-08-12T20:16:24Z")

</div>

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
