从rails

时间:2015-04-30 03:54:40

标签: ruby-on-rails ruby forms nested-forms nested-attributes

我使用了21222015175722792663141RailsGuide的答案组合来尝试从表单中创建多个相互依赖的模型。< / p>

在我的应用中,Users用于身份验证,CliniciansPatients是两种不同类型Users的模型。 CliniciansPatients几乎没有共同的属性,因此创建单独的模型是有意义的。

我希望能够在表单上同时创建patientclinicianuserPatientsclinicians都通过user整数字段连接到user_id

现在patientsclinicians belongs_to一个userpatientsbelongs_to一个clinician和一个{{1 }} clinician has_many):

patients

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 user has_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 在new.html.erb - 病人页面上创建userpatient。我按照RailsCast Nested Model Form Part 1进行了操作,因为它被建议作为问题10058584的答案。这是我的表格:

accepts_nested_attributes_for

<%= 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 字段的表单:useremail。提交后,我创建了一个新用户,但没有创建患者。

如何让password字段显示在表单上,​​并创建patientpatient,其user_id将其连接到同一个user时间?

最终,我需要在创建patient时创建clinicianuser;也许通过制作patient/clinician accepts_nested_attributes_for一个user并为每个人制作表单?

非常感谢任何建议,谢谢

1 个答案:

答案 0 :(得分:1)

我认为,为了显示您的patient表单字段,您需要在@user.build_patient的{​​{1}}操作中说new。这会初始化与您创建的用户实例关联的患者。

如果它对其他人有帮助,如果患者和用户之间的关系是UsersController而不是has_many,那么语法将是has_one @user.patient.build行为

相关问题