Rails 3 Nested Forms

前端 未结 2 2052
既然无缘
既然无缘 2020-12-25 09:47

I have a Person model and an Address Model:

class Person < ActiveRecord::Base
  has_one :address
  accepts_nested_attributes_for :address
end


class Addr         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-25 10:00

    Why is your address built in your new action, and not in the create action? You're building an address from a non saved model, without an id, so the foreign key can't be set. You should keep your @person in your new action, but put your build_address in your create action, after @person has been saved.

提交回复
热议问题