Reputation: 2879
I'm trying to use update_attributes and accepts_nested_attributes_for to update some children objects (days_events) from the parents (location) controller.
Everything looks good in params hash but when I attempt to use assign_attributes or update_attributes the actual dates don't get updated.The first days_event record is created in the database successfully as it should with the event_id and location_id but the start_date and end_date fields are empty.
There aren't any model restrictions on the date fields either.
Here's the code in my location(parent) controller:
def update
logger.debug('PARAMS HASH********'+params.to_s)
@total_forms=params[:total_forms].to_i
@current_form=params[:current_form].to_i
@location = Location.find(params[:id])
@location.update_attributes(params[:location])
end
Here's my params hash:
{
"utf8"=>"Γ£ô",
"_method"=>"put",
"authenticity_token"=>"Mz2GzRmqxrygy01d2LdkE6ZZY+fJK1PSlVhFKlVUme8=",
"location"=>{
days_events_attributes"
=>{
"0"=>{
"start_date"=>"03/14/2013",
"end_date"=>"03/14/2013",
"_destroy"=>"false",
"event_id"=>"588"
}, "
1"=>{"
start_date"=>"",
"end_date"=>"",
"_destroy"=>"1",
"event_id"=>"588"
},
"2"=>{
"start_date"=>"",
"end_date"=>"",
"_destroy"=>"1",
"event_id"=>"588"
}
}
},
"total_forms"=>"2",
"current_form"=>"0",
"commit"=>"Add Date s for Next Location",
"action"=>"update",
"controller"=>"locations",
"id"=>"871"
}
Upvotes: 1
Views: 321
Reputation: 576
Check the date format, MySQL for example uses the YYYY-MM-DD format, if you input an invalid format it will just reject it.
If you want to normalize the date format regardless of what you get as input, I would suggest adding a before_validation
hook to your model and do it there. Check the answer on this question for a suggestion, it doesn't deal with dates (it shows how to format a price attribute) but it should give you an idea of what you need to do.
Should you need help formatting the date drop a comment here and I'll get to it as soon as I can.
Hope it helps :)
Upvotes: 2