has_many:through和collection_select rails

时间:2015-09-02 03:15:33

标签: ruby-on-rails ruby forms has-many-through collection-select

我已经尝试过针对类似问题的所有解决方案,并且没有弄明白这一点。

我有一个has_many:通过'临床医生'​​和'患者'之间的关系,加入模型'CareGroupAssignment'。到目前为止,我尝试过的方法都没有能够将clinician保存到patient关联。我希望patient能够将多个clinicians与之关联,clinicians将有多个patients

clinician.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

patient.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 %>

接下来,我尝试了对this questioncollection_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>

最后,我复制了this questions/solution中的内容。也没有格式化为普通的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>

到目前为止,这些方法都没有将clinician保存到patient关联。

patient_controller.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页面上显示与patient相关联的show

患者 show.html.erb

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

如果我为数据库设定种子,但由于数据似乎没有存储,所以没有任何内容显示。

Rails 4.1.8,ruby 2.2.1p85,PostgreSQL

由于

1 个答案:

答案 0 :(得分:0)

another question上找到答案我问:

问题在于控制器中的这一行:

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

应该是:

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