简体   繁体   English

从Rails中的一种形式创建两个相互依赖的模型

[英]Create two interdependent models from one form in rails

I have used a combination of answers from 21222015 , 17572279 , 2663141 , and RailsGuide to try and get multiple interdependent models to be created from a form. 我使用的答案组合从21222015175722792663141 ,和RailsGuide尝试并获得从表单创建多个相互依存的模型。

In my application Users are used for authentication and Clinicians and Patients are models for the two different types of Users . 在我的应用程序中, Users用于身份验证, CliniciansPatients是两种不同类型Users Clinicians and Patients have almost no attributes in common so creating separate models made sense. CliniciansPatients几乎没有共同点,因此创建单独的模型是有意义的。

I would like to be able to create a patient or clinician and a user at the same time on a form. 我希望能够同时在表格上创建patientclinician以及user Patients and clinicians are both connected to their user by a user_id integer field. Patientsclinicians都通过user_id整数字段连接到其user

Right now patients and clinicians belongs_to a user ( patients also belongs_to a clinician and a clinician has_many patients ): 目前, patientsclinicians belongs_to userpatientsbelongs_to clinicianclinician 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 

A user has_one patient or clinician : user has_one patientclinician

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

I am trying to create a user and a patient on the new.html.erb - patients page using accepts_nested_attributes_for . 我正在尝试使用accepts_nested_attributes_for在new.html.erb-患者页面上创建userpatient I followed the RailsCast Nested Model Form Part 1 as it was recommended as an answer to SO question 10058584 . 我遵循了RailsCast 嵌套模型表单第1部分,因为它被建议作为SO问题10058584的答案。 This is my form: 这是我的表格:

   <%= 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 %>

In my UsersController I have: 在我的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

All of this currently gives me a form that only shows the two user fields: email and password . 目前,所有这些都为我提供了一个仅显示两个user字段的表格: emailpassword When submitted I get a new user created but no patient is created. 提交后,我创建了一个新用户,但没有创建任何患者。

How do I get the patient fields to appear on the form and have a patient created that has a user_id that connects it to the user created at the same time? 如何获取patient字段显示在表单上,​​并创建一个具有user_idpatient ,该patient user_id将其与同时创建的user连接?

Eventually I need to have it possible to create either a patient or a clinician when a user is created; 最终,我需要在创建user时创建patientclinician maybe by making it so that that a patient/clinician accepts_nested_attributes_for a user and have the form for each? 也许是这样做的,以便patient/clinicianuser接受accepts_nested_attributes_for并为每个user提供表格?

Any advice would be really appreciated, thanks 任何建议,将不胜感激,谢谢

I think that in order for your patient form fields to show up, you need to say @user.build_patient in your new action in your UsersController . 我认为,为了显示您的patient表格字段,您需要在UsersController中的new操作中说@user.build_patient This initializes a patient that is associated with the User instance you created. 这将初始化与您创建的User实例关联的患者。

In case it helps anyone else, if the relationship between patient and user was a has_many instead of a has_one , then the syntax would be @user.patient.build the new action. 万一它对其他人有帮助,如果患者和用户之间的关系是has_many而不是has_one ,则语法为@user.patient.build new动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM