# How do I get a javascript variable into rails

**URL:** <https://www.ruby-forum.com/t/how-do-i-get-a-javascript-variable-into-rails/133376>\
**Category:** Rails\
**Created:** [March 31, 2008, 1:19am UTC](https://www.ruby-forum.com/t/how-do-i-get-a-javascript-variable-into-rails/133376 "2008-03-31T01:19:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@sweiss](https://www.ruby-forum.com/u/sweiss)\
**Post date:** [March 31, 2008, 1:19am UTC](https://www.ruby-forum.com/t/how-do-i-get-a-javascript-variable-into-rails/133376/1 "2008-03-31T01:19:42Z")

</div>

I have looked around and I am either not understanding something, or  
just can’t find a way to get a javascript variable posted to into  
rails. I have a javascript that runds and takes input from a user, and  
I need to get these javascript values into the rails app on the  
server. Since this is within a script the ruby link\_to\_remote and  
alike functions won’t work since I need to be within the javascript  
script, so I need an example of a javascript/prototype ajax function  
or just a submit or post and how I can read variables passed between  
on the server side.

Any help would be much obliged

---

<div class="post-metadata">

**Author:** ![sweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@sweiss](https://www.ruby-forum.com/u/sweiss)\
**Post date:** [March 31, 2008, 1:37am UTC](https://www.ruby-forum.com/t/how-do-i-get-a-javascript-variable-into-rails/133376/2 "2008-03-31T01:37:18Z")

</div>

You can make a direct ajax call

var myvar = ‘something’;  
var url = ‘/some\_controller/some\_action?  
myvar=’+encodeURIComponent(myvar);  
new Ajax.Request(url,  
{ asynchronous:true, evalScripts:true, method:‘get’,  
onComplete: function(t) { // do something  
}  
});

---

<div class="post-metadata">

**Author:** ![sweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@sweiss](https://www.ruby-forum.com/u/sweiss)\
**Post date:** [March 31, 2008, 1:56am UTC](https://www.ruby-forum.com/t/how-do-i-get-a-javascript-variable-into-rails/133376/3 "2008-03-31T01:56:39Z")

</div>

Thanks, and I have seen some things kind of like this, but what I  
can’t find is the server side code,

but how do I read that myvar in the controller

---

<div class="post-metadata">

**Author:** ![sweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@sweiss](https://www.ruby-forum.com/u/sweiss)\
**Post date:** [March 31, 2008, 2:55am UTC](https://www.ruby-forum.com/t/how-do-i-get-a-javascript-variable-into-rails/133376/4 "2008-03-31T02:55:18Z")

</div>

That code snippet with send an http request to the specified url (which  
will be routed to a controller)…

so

var content = ‘something’;  
var url = ‘/posts/create?  
message=’+encodeURIComponent(content);  
new Ajax.Request(url,  
{ asynchronous:true, evalScripts:true, method:‘get’,  
onComplete: function(t) { // do something  
}  
});

Let’s say that following goes to posts/create and it has the variable  
attached to it so the full url is:

/posts/create?message=something

This will end an ajax request to the “create” action of the “posts”  
controller (assuming the routes) and the “message” param appended at the  
end will be accessible in the action via params[:message]

so inside /app/controllers/posts\_controller.rb

def create  
Post.new(params[:message])

# params[:message] =\> ‘something’

end

---

<div class="post-metadata">

**Author:** ![sweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@sweiss](https://www.ruby-forum.com/u/sweiss)\
**Post date:** [March 31, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/how-do-i-get-a-javascript-variable-into-rails/133376/5 "2008-03-31T14:19:37Z")

</div>

You will probably save yourself some effort here if you create a js  
function that you include in the page (e.g., application.js or another  
js file you include) and let that function accept the url or an  
options hash through which you pass the url. That way you can rely on  
Rails’ helpers to build the url for you, decoupling your js from the  
Routing system currently used.

As for the Ajax.Request, the posted methods will work but they are a  
bit messy. Prototype’s method actually has a ‘parameters’ option that  
handles the variables in a cleaner fashion. For example:

my\_update = function(url) {  
…  
new Ajax.Request(url, parameters: {var\_1: encodeURICompoment(var1),  
var\_2: encodeURIComponent(var2), …}, asynchronous:true,  
evalScripts:true, method: ‘put’}  
}

---

<div class="post-metadata">

**Author:** ![sweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@sweiss](https://www.ruby-forum.com/u/sweiss)\
**Post date:** [March 31, 2008, 3:36am UTC](https://www.ruby-forum.com/t/how-do-i-get-a-javascript-variable-into-rails/133376/6 "2008-03-31T03:36:55Z")

</div>

thanks for the help

On Mar 30, 7:55 pm, Nathan E. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![sweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@sweiss](https://www.ruby-forum.com/u/sweiss)\
**Post date:** [March 31, 2008, 3:02pm UTC](https://www.ruby-forum.com/t/how-do-i-get-a-javascript-variable-into-rails/133376/7 "2008-03-31T15:02:33Z")

</div>

On 31 Mar 2008, at 13:19, AndyV wrote:

> You will probably save yourself some effort here if you create a js  
> function that you include in the page (e.g., application.js or another  
> js file you include) and let that function accept the url or an  
> options hash through which you pass the url. That way you can rely on  
> Rails’ helpers to build the url for you, decoupling your js from the  
> Routing system currently used.

Also don’t forget about the :with option  
(eg link\_to\_remote ‘Click me’, :url =\> {…}, :with =\> ‘put some  
javascript here’)

Fred
