passing variables between rails and javascript

Lee Thompson wrote:

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 ******* <script type="text/javascript">     function javascript_functionA(items){        //do things with items     } </script> <% 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 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

Lee Thompson wrote:

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.

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

Long