Passing an array into controller

I am trying to pass and array from an AJAX Javascript function to a
controller. The parameter is being read as a single string and not an
array.

The Javascript function:
function sendtolist(formName){
var form = $(formName)
var checked = []
checked = form.getInputs(“checkbox”,“plans[]”).findAll(function(item)
{ return item.checked; }).pluck(“value”);
action=“/generate_reports/advanced_plan_summary”
window.location.href=action+“?plans[]=”+checked
}

The controller is:

def advanced_plan_summary
@ids = params[:plans]
@ids = @ids.split(‘,’)
@plans = Plan.find(@ids.to_a)
if @plans[0].nil?
flash[:warning] = “Advanced Plan Selection Empty!!!”
redirect_to(:action => :index)
end
@planname = []
for plan in @plans
@planname << " " + plan.plan_name
end
flash[:notice] = “Plans picked = #{@planname}”
#…
end

I select plans with the id 58 and 32. It only seems to find the item
58???

Here is what my development log looks like:

Processing GenerateReportsController#process_state (for 127.0.0.1 at
2008-09-15 17:23:55) [POST]
Session ID: e78dc6d1d0552aa6551bd18996c5c48e
Parameters: {“action”=>“process_state”,
“controller”=>“generate_reports”}
User Load (0.002074) SELECT * FROM users WHERE (id = 25) LIMIT 1
Completed in 0.00387 (258 reqs/sec) | Rendering: 0.00006 (1%) | DB:
0.00606 (156%) | 200 OK [http://127.0.0.1/generate_reports/
process_state]
User Columns (0.003930) SHOW FIELDS FROM users

Processing GenerateReportsController#advanced_plan_summary (for
127.0.0.1 at 2008-09-15 17:23:55) [GET]
Session ID: e78dc6d1d0552aa6551bd18996c5c48e
Parameters: {“plans”=>[“58,32”], “action”=>“advanced_plan_summary”,
“controller”=>“generate_reports”}
User Load (0.002175) SELECT * FROM users WHERE (id = 25) LIMIT 1
Plan Columns (0.002646) SHOW FIELDS FROM plans
Plan Load (0.001551) SELECT * FROM plans WHERE (plans.id = 58)
Rendering within layouts/application
Rendering generate_reports/advanced_plan_summary
Completed in 0.04176 (23 reqs/sec) | Rendering: 0.02336 (55%) | DB:
0.01030 (24%) | 200 OK [http://127.0.0.1/generate_reports/
advanced_plan_summary?plans[]=58,32]

Has anyone had experience with passing an array of objects and how
this is supposed to work.
Thanks
William

On 15 Sep 2008, at 23:26, Will wrote:

{ return item.checked; }).pluck(“value”);
action="/generate_reports/advanced_plan_summary"
window.location.href=action+"?plans[]="+checked
}

You need to make a query string that looks like
plans[]=53&plans[]=52&plans[43] (to send the array [53, 52, 43])

Fred

Thanks that worked for me. I would love to change this so I’m using a
link_to object ruby object to call this function and pass the results
to my controller rather than calling it from the Javascript object?

Here is how I ended up doing it

window.location.href=action+“?”+array_params(checked);

function array_params(arry) {
//Prototype magic… concatenating array onto variable string…
var paramvar = “”;
arry.each(function(s){
paramvar = paramvar.concat(“plans[]=”,s,“&”);});
paramvar = paramvar.replace(/&$/,“”);
return paramvar;
}

It returned - plans[]=53&plans[]=52&plans[]=42

Thanks again.
Will

On Sep 15, 6:05 pm, Frederick C. [email protected]