# Passing a JavaScript variable through a Rails params hash

**URL:** <https://www.ruby-forum.com/t/passing-a-javascript-variable-through-a-rails-params-hash/168724>\
**Category:** Rails\
**Created:** [May 25, 2009, 2:40am UTC](https://www.ruby-forum.com/t/passing-a-javascript-variable-through-a-rails-params-hash/168724 "2009-05-25T02:40:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [May 25, 2009, 2:40am UTC](https://www.ruby-forum.com/t/passing-a-javascript-variable-through-a-rails-params-hash/168724/1 "2009-05-25T02:40:17Z")

</div>

Hi guys, hoping someone can give me a pointer with this?

I have a Javascript variable that I’m updating dynamically as a user  
interacts with the page. I want the user to be able to save it’s value  
at any time along with some text data in a pop-up form, so the best way  
I can think of is to pass it through the params hash without writing it  
into the form. I’m not totally sure how to do this though?

The only practical solution i’ve got is for a hidden field line in the  
form to be dynamically generated when the user goes to save it, but this  
feels like overkill to me. Does anyone have any suggestions for a neater  
way to do it?

Thanks,

Jon

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [May 25, 2009, 4:04am UTC](https://www.ruby-forum.com/t/passing-a-javascript-variable-through-a-rails-params-hash/168724/2 "2009-05-25T04:04:16Z")

</div>

Hi

You need to change the target attrubute of the form tag. Give it an id  
if it doesn’t have one:  
form\_tag({:action =\>“update”}, {:id =\> “the\_form”}) do  
…  
Then if using prototype, $(‘the\_form’).target += ‘&varname=’+var;

Mind you URL encode the params value. You can use the escapeHTML  
string class to encode it properly if using prototype. I think there  
is also an escape function 😉

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
New video up now at [http://sensei.zenunit.com/](http://sensei.zenunit.com/) real fastcgi rails  
deploy process! Check it out now!

On 25/05/2009, at 10:40 AM, Jon H. \<rails-mailing-list@andreas-
