# Nested form gets inserted twice on every create

**URL:** <https://www.ruby-forum.com/t/nested-form-gets-inserted-twice-on-every-create/187771>\
**Category:** Rails\
**Created:** [April 29, 2010, 2:16am UTC](https://www.ruby-forum.com/t/nested-form-gets-inserted-twice-on-every-create/187771 "2010-04-29T02:16:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![walt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/71e660/32.png) [@walt](https://www.ruby-forum.com/u/walt)\
**Post date:** [April 29, 2010, 2:16am UTC](https://www.ruby-forum.com/t/nested-form-gets-inserted-twice-on-every-create/187771/1 "2010-04-29T02:16:10Z")

</div>

Hi,

I’m trying to ‘add appointments with schedules’

so I have

class Appointment \< ActiveRecord::Base  
has\_many :schedules  
accepts\_nested\_attributes\_for :schedules, :allow\_destroy =\> true  
end

and

class Schedule \< ActiveRecord::Base  
has\_paper\_trail  
belongs\_to :appointment  
end

and my AppointmentsController has this create action

def create  
resource = new\_resource  
if resource.update\_attributes(params[:appointment])  
flash[:notice]= “Perfect”  
else  
flash[:warning]= “Errors”  
end  
end

and my appointments/new.html.erb looks like

\<% semantic\_form\_for resource,  
:html =\> { :class =\> “oxform” },  
:url =\> resources\_url do |form| %\>  
\<%= render :partial =\> “/appointments/form”, :locals =\> { :form =\>  
form } %\>  
\<% form.buttons do %\>  
  
\<% end %\>  
\<% end %\>

Finally my \_form.html.erb’s are like

appointments/\_form.html.erb:

\<% form.inputs :appointment\_form do %\>  
\<%= form.input :title %\>  
\<% form.semantic\_fields\_for :fields do |fld| %\>  
\<%= fld.input :location %\>  
\<%= fld.input :category %\>  
\<% end %\>

- 
 \<% form.fields\_for :schedules do |builder| %\> \<%= render 'schedule', :fld =\> builder %\> \<% end %\> - \<%= link\_to\_add\_schedule("Add Schedule", form, :schedules) %\> 

appointments/\_schedule.html.erb:

- 
 \<%= fld.input :first\_date, :as =\> :string %\> \<%= fld.input :last\_date, :as =\> :string %\> \<%= fld.input :all\_day\_event, :as =\> :oxboolean %\> \<%= fld.input :repeating, :as =\> :oxboolean %\> \<%= fld.hidden\_field :id %\> \<%= fld.hidden\_field :\_destroy %\> \<%= link\_to\_function "Drop Schedule", "removeSchedule(this)" %\> 

You in ‘the know’ already have spotted that I’m trying to use Formtastic  
and Ian W.s fabulous resources\_controller -

Like I said in the subject: when I POST the appointments/new - I get two  
schedules added every time - even though the params look like this:

Processing AppointmentsController#create (for 127.0.0.1 at 2010-04-28  
13:20:00) [POST]  
Parameters: {“appointment”=\>{“title”=\>“og endnu et”,  
“schedules\_attributes”=\>{“0”=\>{“first\_date”=\>"", “last\_date”=\>""}}},  
“authenticity\_token”=\>“RO3cWfhF+2wfH6hXzG0HwDByHLcxl9yAOMBLPk0PRaI=”,  
“calendar\_id”=\>“16”}

Every thing is fine - I just get that extra schedules added to my  
schedules table - as a way for Rails 2.3.5 to say “thank you” 🙂

Have I involuntarily stepped on a bug - most likely I’ve just mixed gems  
and plugins that do not like each other/Rails 2.3.5, or left the obvious  
‘some statement’ untouched, grinning back at me from somewhere deep  
within vendor/…

Please if someone has the upperhand on this one - throw me rope \<:)

Cheers,  
Walther
