I'm constructing some data to send over jQuery's $.post, the data that I send looks like this:
authenticity_token: LxHlgi1WU8o0DtaNuiQOit/e+HlGR2plVcToHUAhA6I=
crews[0][boat_id]: 2
crews[0][crew][]: 10
end_time: 1280408400
start_time: 1280404800
Which is from jQuery converting the data object which is prepared by:
- creating an array [] for each 'crew' found in the DOM, called crews.
- Then I cycle over the crews in the DOM, create an object and store its boat ID
- then I create another array for its a crews list of members, which is then added into the crew's object and pushed onto the 'crews' array. (The JS is a bit long, can post if needed)
Then in my controller I have:
def create
@outing = Outing.create(
:club => current_user.club,
:title => 'Outing',
:start_time => Time.at(params[:start_time].to_i),
:end_time => Time.at(params[:end_time].to_i)
)
@outing.save
Rails.logger.debug(params[:crews].inspect)
params[:crews].each_with_index do |crew,i|
Rails.logger.debug(crew.inspect)
@crew = Crew.create(
:boat_id => crew[:boat_id].to_i,
:outing_id => @outing.id
)
@crew.save
crew[:crew].each do |c|
@cu = CrewUsers.create(
:crew_id => @crew.id,
:user_id => c.to_i
)
end
end
render :nothing => true
end
The two inspect statements print something like this:
{"0"=>{"crew"=>["10"], "boat_id"=>"2"}}
["0", {"crew"=>["10"], "boat_id"=>"2"}]
and I get this error:
TypeError (Symbol as array index):
To me, crews shouldn't be an indexed array, and that's where the issue is. But I could also be totally wrong. Any ideas on what I'm doing wrong or how to fix it?