2

我已经尝试了所有类似问题的解决方案,但还没有弄清楚这一点。

我有一个has_many:通过'Clinician'和'Patient'与加入模型'CareGroupAssignment'之间的关系。到目前为止,我尝试过的所有方法都无法保存cliniciantopatient关联。我希望patient能够有多个clinicians与之关联,并且clinicians将有多个patients.

临床医生.rb(简化)

class Clinician < ActiveRecord::Base
    belongs_to :care_group

    has_many :patients ,:through=> :care_group_assignments
    has_many :care_group_assignments, :dependent => :destroy

    belongs_to :user
    accepts_nested_attributes_for :user,  :allow_destroy => true
end

病人.rb

class Patient < ActiveRecord::Base
    belongs_to :care_group

    has_many :clinicians ,:through=> :care_group_assignments
    has_many :care_group_assignments

    belongs_to :user
    accepts_nested_attributes_for :user,  :allow_destroy => true
end

care_group_assignments.rb

class CareGroupAssignment < ActiveRecord::Base
    belongs_to :clinician
    belongs_to :patient
end

我首先尝试按照Railscasts PRO #17-HABTM Checkboxes中的示例至少开始收集数据并正确设置模型。下面是带有 RailsCast 中描述的每个临床医生的复选框的表格,复选框显示并且数据已发送但未存储(无法弄清楚原因)。

患者new.html.erb表格

<%= form_for @patient do |form| %>

  <%= form.fields_for :user do |builder| %>
   <div class="form-group">
      <%= builder.label "Email or Username" %>
      <%= builder.text_field :email, class: "form-control" %>
    </div>
    <div class="form-group">
      <%= builder.label :password %>
      <%= builder.password_field :password, class: "form-control" %>
    </div>
  <% end %>

  <div class="form-group">
    <%= form.label :first_name %>
    <%= form.text_field :first_name, class: "form-control", placeholder: "First name" %>
  </div>

  <div class="form-group">
    <%= form.label :last_name %>
    <%= form.text_field :last_name, class: "form-control", placeholder: "Last name" %>
  </div>

  <div class="form-group">
    <% Clinician.where(care_group_id: @care_group.id).each do |clinician| %>
      <%= check_box_tag "patient[clinician_ids][]", clinician.id, @patient.clinician_ids.include?(clinician.id), id: dom_id(clinician) %>
      <%= label_tag dom_id(clinician), clinician.full_name %><br>
    <% end %>
  </div>

  <%= form.button 'Create Patient', class: "btn btn-u btn-success" %>
<% end %>

接下来,我尝试了这个问题collection_select的答案。这会创建一个格式错误的列表,其中只能选择一个。数据似乎已发送,但再次没有保存。clinician

患者new.html.erb表格

  <div class="form-group">
    <%= collection_select(:patient, :clinician_ids, 
      Clinician.where(care_group_id: @care_group.id).order("first_name asc"), 
      :id, :full_name, {:selected => @patient.clinician_ids, :include_blank => true}, {:multiple => true}) %>
  </div>

最后,我复制了在这个问题/解决方案中所做的事情。也没有格式化为正常的collection_select下拉列表,而是一个列表,周围有一个边框,只能clinician选择一个。

患者new.html.erb表格

  <div class="form-group">
    <% Clinician.where(care_group_id: @care_group.id).each do |clinician| %>
      <%= check_box_tag "patient[clinician_ids][]", clinician.id, @patient.clinician_ids.include?(clinician.id), id: dom_id(clinician) %>
      <%= label_tag dom_id(clinician), clinician.full_name %><br>
    <% end %>
  </div>

到目前为止,这些方法中没有一个能够保存cliniciantopatient关联。

病人控制器.rb

def new
  @patient = Patient.new
  @user = User.new
  @patient.build_user

  @care_group = current_clinician.care_group
end

def create
  @patient = Patient.create(patient_params)
  @patient.care_group = current_clinician.care_group

  if @patient.save
    redirect_to patient_path(@patient), notice: "New patient created!"
  else
    render "new"
  end
end

def show
 @patient = Patient.find_by(id: params["id"])
end

private
 def patient_params
  params.require(:patient).permit({:clinician_ids => [:id]},:first_name,:last_name,:user_id,:care_group_id, user_attributes: [ :email, :password, :patient_id, :clinician_id ])
 end

我计划在患者页面上显示clinicians与 a 相关的:patientshow

病人show.html.erb

<strong>Shared with:</strong>
 <% @patient.clinicians.each do |clinician| %>
  <%= clinician.full_name %><
 <% end %>

如果我为数据库播种,则此方法有效,但由于似乎没有存储数据,因此没有显示任何内容。

Rails 4.1.8,红宝石 2.2.1p85,PostgreSQL

谢谢

4

1 回答 1

0

在我问的另一个问题上找到了答案:

问题是控制器中的这一行:

params.require(:patient).permit({:clinician_ids => [:id]}...

它应该是:

params.require(:patient).permit({:clinician_ids => []}...

于 2015-09-04T01:24:00.890 回答