# Passing variables between rails and javascript

**URL:** <https://www.ruby-forum.com/t/passing-variables-between-rails-and-javascript/72157>\
**Category:** Rails\
**Created:** [October 14, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/passing-variables-between-rails-and-javascript/72157 "2006-10-14T23:32:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [October 14, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/passing-variables-between-rails-and-javascript/72157/1 "2006-10-14T23:32:35Z")

</div>

Hi,

I’ve been looking around on the forums and tutorials but I still can’t  
seem to make this work.

Here is what I am trying to do.  
I have a view do a form\_remote\_tag with some variables. In the  
controller I want to create an array, fill it with values, and when the  
function returns I want to call a javascript function with this array.

currently this is what I have that doesn’t work  
\*\*\*\*\*\*\*\* view \*\*\*\*\*\*\*

\<% form\_remote\_tag :complete =\> "javascript\_functionA(’#{myArray}’),  
:url =\> {:action =\> :controller\_functionA} %\>

\*\*\*\*\*\*\*\* controller \*\*\*\*\*\*\*  
class MyController \< ApplicationController  
def controller\_functionA  
myArray = [“so”, “many”, “exciting”, “things”]  
end  
end

I know I must be doing something wrong but being new to rails there just  
seems like to many places where I could be messing things up.

Thanks for the help,  
Lee

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [October 14, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/passing-variables-between-rails-and-javascript/72157/2 "2006-10-14T23:51:05Z")

</div>

Lee Thompson wrote:

> \*\*\*\*\*\*\*\* controller \*\*\*\*\*\*\*  
> class MyController \< ApplicationController  
> def controller\_functionA  
> myArray = [“so”, “many”, “exciting”, “things”]  
> end  
> end

I don’t know what parameters are valid for javascript\_functionA(), but  
it  
may be expecting (‘value1’, ‘value2’, … ). Looks like ‘#{myArray}’ is  
giving  
just one parameter (‘value1’).

You may have to reference each array value by its indices.

Long

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [October 15, 2006, 12:00am UTC](https://www.ruby-forum.com/t/passing-variables-between-rails-and-javascript/72157/3 "2006-10-15T00:00:34Z")

</div>

Long wrote:

> Lee Thompson wrote:
> 
> > \*\*\*\*\*\*\*\* controller \*\*\*\*\*\*\*  
> > class MyController \< ApplicationController  
> > def controller\_functionA  
> > myArray = [“so”, “many”, “exciting”, “things”]  
> > end  
> > end
> 
> I don’t know what parameters are valid for javascript\_functionA(), but  
> it  
> may be expecting (‘value1’, ‘value2’, … ). Looks like ‘#{myArray}’ is  
> giving  
> just one parameter (‘value1’).
> 
> You may have to reference each array value by its indices.
> 
> Long

Thanks for the response Long,

You might be right about that… but for now I actually get this error  
undefined local variable or method `myArray’ for  
#\<#Class:0x409c9ec4:0x409c9e9c\>

The array never makes it to the javascript\_functionA

the MyController creates myArray, but the view has no idea about it.

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [October 15, 2006, 1:21am UTC](https://www.ruby-forum.com/t/passing-variables-between-rails-and-javascript/72157/4 "2006-10-15T01:21:10Z")

</div>

Lee Thompson wrote:

> > >
> 
> Thanks for the response Long,
> 
> You might be right about that… but for now I actually get this error  
> undefined local variable or method `myArray’ for  
> #\<#Class:0x409c9ec4:0x409c9e9c\>
> 
> The array never makes it to the javascript\_functionA
> 
> the MyController creates myArray, but the view has no idea about it.

Right, try make myArray an instance variable (@myArray) instead of a  
local variable.  
Then reference @myArray in your view instead of myArray.

Long
