ruby on rails - accepts_nested_attributes_for building form -


i getting error: undefined method build nil:nilclass when trying build empty child object form.

class patientscontroller < applicationcontroller   def index   end    def new     @patient = patient.new      # causes error (undefined method `build' nil:nilclass)     @patient.user.build    end end      class patient < activerecord::base   belongs_to :user   accepts_nested_attributes_for :user   attr_accessible :user_id, :user_attributes end  # == schema information # # table name: patients # #  id         :integer         not null, primary key #  user_id    :integer #  created_at :datetime #  updated_at :datetime # 

since patient belongs user need build patient user.

@user.patients.build(params[:patient]) 

patient.new used create blank instance of patient can render on new form, when posting create need build user.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -