我有 2 个模型,一个用户和一个患者.一个用户有一个患者,一个患者 BELONGS_TO 一个用户.
I have 2 models, a User and Patient. A User HAS_ONE Patient and a Patient BELONGS_TO a 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 my patients controller I am trying to create a new Patient form.
class PatientsController < ApplicationController def new @patient = Patient.new end end在我的视图中 (new.html.erb)
In My View (new.html.erb)
<%= form_for @patient do |patient_form| %> <% patient_form.fields_for :user do |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%>表单显示为空白,带有一个提交按钮,没有为 user_fields 生成标记
The form shows up blank with a submit button with no generated markup for the user_fields
我被告知我这样做是错误的,因为患者有 accepts_nested_attributes_for :user 并且它应该是用户在我的系统中嵌套属性但我想使用资源模型,以便治疗患者和其他用户类型分开.
I have been told I am doing this wrong because of the patient having the accepts_nested_attributes_for :user and it should be the user nesting the attributes BUT in my system I want to use the resources model so a patient and other user types are treated separately.
示例数据库表:
用户:id|first_name|last_name...等
USERS: id|first_name|last_name...etc
患者:id|user_id|保险
PATIENTS: id|user_id|insurance
推荐答案除非我弄错了,当您调用 fields_for 时,您没有 user.在您可以执行 fields_for 之前,您需要有一个可用于构建表单的用户实例,有点像您的 @patientpatient_form.
Unless I'm mistaken, you have no user when you are calling fields_for. Before you can do the fields_for, you will need to have an instance of a user that can be used to build the form, kind of like how you have @patient for your patient_form.
最好的办法是根据您的 @patient 在您的控制器中构建一个 User,然后您就可以在视图中访问它.
Your best bet would be to build a User in your controller, based off of your @patient, and then you will have access to that in the view.
更多推荐
使用 accepts
发布评论