ruby - Rails 3. Why is my nested record deleted when I go to the Edit page? -


every company supposed have 1 companycontact. company form has fields company contacts. when update company , add new company contact, works fine, because in show page company, show new company contact. when click edit link takes me edit page (note: don't click update button yet), in edit company form companycontact supposed blank. check logs , companycontact deleted.

delete "company_contacts" "company_contacts"."id" = ? [["id", 4]]

i'm confused because haven't called delete action.

---------------------------------------- company.rb has_one :company_contact, :dependent => :destroy accepts_nested_attributes_for :company_contact  ---------------------------------------- company_contact.rb belongs_to :company  ---------------------------------------- companies_controller.rb def new   @company = company.new   company_contact = @company.build_company_contact   respond_to |format|     format.html # new.html.erb     format.json { render json: @company }   end end  def edit   @company = company.find(params[:id])   company_contact = @company.build_company_contact end 

in edit action you're building company contact company, company has 1 company contact. check existence before building new one:

company_contact = @company.company_contact || @company.build_company_contact 

Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -