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