【发布时间】:2015-04-30 03:54:39
【问题描述】:
我使用了来自21222015、17572279、2663141 和RailsGuide 的答案组合来尝试从一个表单中创建多个相互依赖的模型。
在我的应用程序中,Users 用于身份验证,Clinicians 和 Patients 是两种不同类型的 Users 的模型。 Clinicians 和 Patients 几乎没有共同的属性,因此创建单独的模型是有意义的。
我希望能够在表单上同时创建patient 或clinician 和user。 Patients 和 clinicians 都通过 user_id 整数字段连接到它们的 user。
现在patients和cliniciansbelongs_touser(patients也belongs_toclinician和clinicianhas_manypatients):
class Patient < ActiveRecord::Base
belongs_to :clinician
belongs_to :user
def set_user_id_from_user
patient.user_id = user.id
end
before_validation :set_user_id_from_user
end
userhas_onepatient 或clinician:
class User < ActiveRecord::Base
has_secure_password
has_one :patient, :dependent => :destroy
has_one :clinician, :dependent => :destroy
accepts_nested_attributes_for :patient, :allow_destroy => true
accepts_nested_attributes_for :clinician, :allow_destroy => true
validates :email, presence: true
validates_uniqueness_of :email
end
我正在尝试使用 accepts_nested_attributes_for 在 new.html.erb - 患者页面上创建 user 和 patient。我关注了 RailsCast Nested Model Form Part 1,因为它被推荐为 SO 问题 10058584 的答案。这是我的表格:
<%= form_for @user do |form| %>
<p>
<div class="form-group">
<%= form.label :email %>
<%= form.text_field :email, class: "form-control", placeholder: "email address" %>
</div>
<div class="form-group">
<%= form.label :password %>
<%= form.password_field :password, class: "form-control", placeholder: "enter password" %>
</div>
</p>
<%= form.fields_for :patient do |builder| %>
<p>
<div class="form-group">
<%= builder.label :first_name %>
<%= builder.text_field :first_name, class: "form-control", placeholder: "First name" %>
</div>
<div class="form-group">
<%= builder.label :last_name %>
<%= builder.text_field :last_name, class: "form-control", placeholder: "Last name" %>
</div>
<div class="form-group">
<%= builder.label :diagnosis %>
<%= builder.text_field :diagnosis, class: "form-control", placeholder: "Diagnosis" %>
</div>
<div class="form-group">
<%= builder.label :gender_id %>
<%= builder.collection_select :gender_id, Gender.all, :id, :gender_type, :prompt => true, class: "form-control" %>
</div>
<div class="form-group">
<%= builder.label :age %>
<%= builder.text_field :age, class: "form-control", placeholder: "Age" %>
</div>
</p>
<% end %>
<%= form.button 'Create Patient', class: "btn btn-u btn-success" %>
<% end %>
在我的UsersController 我有:
def new
@user = User.new
end
def create
@user = User.create(user_params)
if @user.save
redirect_to user_path(@user), notice: "User created!"
else
render "new"
end
end
private
def user_params
params.require(:user).permit(:email, :password, patient_attributes: [ :first_name,:last_name,:user_id,:diagnosis,:gender_id,:age,:address, :email, :password, :phone_number, :caregiver_name, :other_symptom, :goals_of_care, :patient_deceased, :patient_archived ])
end
目前所有这些都为我提供了一个仅显示两个 user 字段的表单:email 和 password。提交后,我创建了一个新用户,但没有创建任何患者。
如何让patient 字段出现在表单上,并创建一个patient,其中user_id 将其连接到同时创建的user?
最终我需要在创建user 时创建patient 或clinician;也许通过使 patient/clinician accepts_nested_attributes_for 和 user 具有每个表单?
非常感谢任何建议,谢谢
【问题讨论】:
标签: ruby-on-rails ruby forms nested-forms nested-attributes