has_many:通过和collection_select导轨形式 [英] has_many :through and collection_select rails form
问题描述
我已经尝试了所有解决类似问题的解决方案,但还没有弄清楚这个问题.
I have tried all of the solutions to similar problems and haven't gotten this one figured out.
我在'Clinician'和'Patient'之间具有has_many:through关系,并具有联接模型'CareGroupAssignment'.到目前为止,我尝试过的所有方法均无法将 clinician
保存为 Patient
关联.我希望有一个病人
与多个关联的 clinicians
,而 clinicians
将具有多个病人
I have a has_many :through relationship between 'Clinician', and 'Patient' with a joined model 'CareGroupAssignment'. None of the methods I have tried so far been able to save the clinician
to patient
association. I would like to have a patient
be able to have multiple clinicians
associated with it and clinicians
will have multiple 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复选框中的示例至少开始收集数据并正确设置模型.下面是带有RailsCast中描述的每个临床医生复选框的表格,该复选框显示,并且数据已发送但未存储(无法查明原因).
I first tried to follow the example from Railscasts PRO #17- HABTM Checkboxes to at least start getting the data collected and to have the models set up correctly. Below is the form with the checkboxes for each clinician as described in the RailsCast, checkboxes show up and the data is sent but not stored (can't figure out why).
患者 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 %>
Next, I tried the collection_select
answer to this question. This creates a badly formatted list where only one clinician
can be selected. The data seems to get sent but again doesn't save.
患者 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>
Lastly, I copied what was done in this questions/solution. Also isn't formatted as a normal collection_select
dropdown but instead a list with a boarder around it where only one clinician
can be selected.
患者 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
关联.
None of these methods have so far been able to save the clinician
to patient
association.
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
我计划在患者显示
页面上显示与患者
相关的临床医生
:
I plan to display the clinicians
associated with a patient
on the patient show
page:
患者 show.html.erb
<strong>Shared with:</strong>
<% @patient.clinicians.each do |clinician| %>
<%= clinician.full_name %><
<% end %>
如果我为数据库添加种子,这将起作用,但是由于似乎没有存储数据,因此没有任何显示.
This works if I seed the database but since the data doesn't seem to be stored, nothing is showing up.
Rails 4.1.8,ruby 2.2.1p85,PostgreSQL
Rails 4.1.8, ruby 2.2.1p85, PostgreSQL
谢谢
推荐答案
problem is this line in the controller:
params.require(:患者).permit({:clinician_ids => [:id]} ...
应该是:
params.require(:患者).permit({:clinician_ids => []} ...
这篇关于has_many:通过和collection_select导轨形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!