ruby on rails - Creating Form with accepts_nested_attributes_for -
i have 2 models, user , patient. user has_one patient , patient belongs_to user.
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 # insurance :string(255) # created_at :datetime # updated_at :datetime # class user < activerecord::base has_one :patient attr_accessible :username, :password, :active, :disabled, :first_name, :last_name, :address_1, :address_2, :city, :state, :postcode, :phone, :cell, :email attr_accessor :password end # == schema information # # table name: users # # id :integer not null, primary key # username :string(255) # encrypted_password :string(255) # salt :string(255) # active :boolean # disabled :boolean # last_login :time # first_name :string(255) # last_name :string(255) # address_1 :string(255) # address_2 :string(255) # city :string(255) # state :string(255) # postcode :string(255) # phone :string(255) # cell :string(255) # email :string(255) # created_at :datetime # updated_at :datetime #
in patients controller trying create new patient form.
class patientscontroller < applicationcontroller def new @patient = patient.new end end
in view (new.html.erb)
<%= form_for @patient |patient_form| %> <% patient_form.fields_for :user |user_fields| %> <table class="formtable" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="label"> <%= user_fields.label :username %> *: </td> <td class="input"> <%= user_fields.text_field :username, :class=>"textfield" %> </td> </tr> </table> ... <%end%> <%end%>
the form shows blank submit button no generated markup user_fields
i have been told doing wrong because of patient having accepts_nested_attributes_for :user , should user nesting attributes in system want use resources model patient , other user types treated separately.
example database table:
users: id|first_name|last_name...etc
patients: id|user_id|insurance
unless i'm mistaken, have no user
when calling fields_for
. before can fields_for
, need have instance of user can used build form, kind of how have @patient
patient_form
.
your best bet build user
in controller, based off of @patient
, , have access in view.
Comments
Post a Comment