Nested form gets inserted twice on every create

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:

    1. <%= 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” :slight_smile:

    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

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs