【问题标题】:Create two interdependent models from one form in rails从 Rails 中的一种形式创建两个相互依赖的模型
【发布时间】:2015-04-30 03:54:39
【问题描述】:

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

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

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

现在patientscliniciansbelongs_touserpatientsbelongs_toclinicianclinicianhas_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_onepatientclinician

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 - 患者页面上创建 userpatient。我关注了 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 字段的表单:emailpassword。提交后,我创建了一个新用户,但没有创建任何患者。

如何让patient 字段出现在表单上,​​并创建一个patient,其中user_id 将其连接到同时创建的user

最终我需要在创建user 时创建patientclinician;也许通过使 patient/clinician accepts_nested_attributes_foruser 具有每个表单?

非常感谢任何建议,谢谢

【问题讨论】:

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


    【解决方案1】:

    我认为,为了让您的 patient 表单字段显示,您需要在 UsersControllernew 操作中输入 @user.build_patient。这会初始化与您创建的用户实例相关联的患者。

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

    【讨论】:

    • 啊,抱歉——has_one 关系与 has_many 关系的语法略有不同,我会更新我的答案。
    • 您知道我将如何制作,以便可以从新用户中创建患者或临床医生吗?现在它只是为一个病人。谢谢
    • 很好,我很高兴它成功了!关于最后一个问题,因为听起来用户要么是患者要么是临床医生(不是两者),如果你想像现在这样使用常规的 ol' rails,我认为最直接的做法是拥有一个 new_patient action 和 new_clinician action,每个都有自己的形式。通过这种方式,您可以初始化正确的用户关系以在表单中进行操作。或者您是说无论用户是临床医生还是患者,表格都将完全相同?
    • 我想我会切换它,以便 user belongs_to clinicianpatient 和他们每个 has_one user;他们翻转一切,以便我使用患者和临床医生控制器并让它们build_user。你觉得这个想法怎么样?我尝试使用jsfiddle.net/w3pGr 来获取它,以便我可以在用户表单之间切换,并且当我选择临床医生作为用户类型但无法显示临床医生表单时,可以让患者部分消失 - 我的 js 技能不存在...
    • 你可以按照你所说的去做,在你翻转关系的地方(所以现在你的user 会有一个user_type,它要么是“临床医生”,要么是“病人”),但是你可以仍然使用您的UsersController,但在对应于user_type 的表单上有一个复选框或下拉列表——这样当您创建用户时,您还将创建与临床医生或患者的正确关联。只要您现在拥有User 模型中的所有属性以便显示表单字段,您就不必再说build_patientbuild_clinician
    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多