Topic: Passing array using post

how would i pass an array using the "post" method?

my code looks like this:
   
    old_count = UnitTask.count
    post :add,:ids=>[1,2,3,4]
    new_count = UnitTask.count   
    assert_equal old_count+4,new_count

I'm trying to emulate this effect:

id[]=1
id[]=2
id[]=3
id[]=4

Kevin

Re: Passing array using post

That looks correct. Are you sure the params hash should have an "ids" key?

Last edited by vwoo (2007-06-20 23:46:19)

Vincent Woo Ruby on Rails Blog

Re: Passing array using post

I made an error in my first post, I am trying to emulate this behavior. Basically, I have a list of check boxes:

ids[]=1
ids[]=2
ids[]=3
ids[]=4

When I put this code in the controller:

logger.error("params[:ids]  #{params[:ids]} ")

the output is this:

params[:ids]  5/6/7

somehow the post is stacking the array into a string, instead of:

ids[]=5&ids[]=6&ids[]=7

Does anyone know to resolve this?

Last edited by surfsupdude (2007-06-21 16:21:59)

Re: Passing array using post

Placing the array in a string like that will flatten it. Try this:

logger.error("params[:ids]  #{params[:ids].inspect} ")

That should look more like an array then. if it doesn't, take a look at the development log and see what parameters are being passed when the action is called.

Railscasts - Free Ruby on Rails Screencasts