Skip to content

Instantly share code, notes, and snippets.

@firebelly
Created April 29, 2011 17:48
Show Gist options
  • Save firebelly/948695 to your computer and use it in GitHub Desktop.
Save firebelly/948695 to your computer and use it in GitHub Desktop.
issue with associated model being duplicated on parent model update
= form_for @school_fair do |f|
- @school_fair.event_times.each_with_index do |et, i|
= render :partial => 'time_slot_fields', :locals => { :event_time => et, :index => i }
= f.submit 'Save'
.event_time
= fields_for "event[event_times_attributes][#{index}]", event_time do |et|
= et.hidden_field :event_id
%li
= et.label :start, "Start Time and Date"
= et.datetime_select :start
%li
= et.label :end_at, "End Time and Date"
= et.datetime_select :end_at
class Event < ActiveRecord::Base
has_many :event_times
accepts_nested_attributes_for :event_times, :allow_destroy => true
end
class EventTime < ActiveRecord::Base
belongs_to :event
end
class SchoolFairsController < ApplicationController
def new
authorize! :manage, Event
@school_fair = Event.new(:event_type_id => EventType.find_by_name('school fair'))
render :layout => 'admin'
end
def edit
authorize! :manage, Event
@school_fair = Event.find(params[:id])
render :layout => 'admin'
end
def update
authorize! :manage, Event
@school_fair = Event.find(params[:id])
if @school_fair.update_attributes(params[:event])
redirect_to edit_school_fair_path(@school_fair), :notice => 'School Fair updated successfully'
else
render :action => 'edit'
end
end
end
Processing by EventsController#update as HTML
Parameters: {
"price"=>[""], "commit"=>"Save", "authenticity_token"=>"...", "utf8"=>"✓", "id"=>"137",
"event"=>{
"name"=>"fall school fair",
"description"=>"<p>\r\n\ttest</p>\r\n",
"event_times_attributes"=>{
"0"=>{"end_at(1i)"=>"2011", "event_id"=>"137", "total_people"=>"55", "end_at(2i)"=>"4", "end_at(3i)"=>"29", "start(1i)"=>"2011", "end_at(4i)"=>"19", "start(2i)"=>"4", "total_children"=>"0", "end_at(5i)"=>"05", "start(3i)"=>"29", "start(4i)"=>"15", "start(5i)"=>"05"},
"1"=>{"end_at(1i)"=>"2011", "event_id"=>"137", "total_people"=>"55", "end_at(2i)"=>"4", "end_at(3i)"=>"29", "start(1i)"=>"2011", "end_at(4i)"=>"19", "start(2i)"=>"4", "total_children"=>"0", "end_at(5i)"=>"05", "start(3i)"=>"29", "start(4i)"=>"15", "start(5i)"=>"05"},
"2"=>{"end_at(1i)"=>"2011", "event_id"=>"137", "total_people"=>"55", "end_at(2i)"=>"4", "end_at(3i)"=>"29", "start(1i)"=>"2011", "end_at(4i)"=>"19", "start(2i)"=>"4", "total_children"=>"0", "end_at(5i)"=>"05", "start(3i)"=>"29", "start(4i)"=>"15", "start(5i)"=>"05"},
"3"=>{"end_at(1i)"=>"2011", "event_id"=>"137", "total_people"=>"55", "end_at(2i)"=>"4", "end_at(3i)"=>"29", "start(1i)"=>"2011", "end_at(4i)"=>"19", "start(2i)"=>"4", "total_children"=>"0", "end_at(5i)"=>"05", "start(3i)"=>"29", "start(4i)"=>"15", "start(5i)"=>"05"}
}
}
}
@firebelly
Copy link
Author

fixed by making sure a hidden field with event_time_id is included in the partial, ala = et.hidden_field :id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment