问题
I have tried all of the solutions to similar problems and haven't gotten this one figured out.
I have a has_many :through relationship between 'Clinician', and 'Patient' with a joined model 'CareGroupAssignment'. I would like to have a patient
be able to have multiple clinicians
associated with it and clinicians
will have multiple patients
.
When I submit my form this is logged:
Started POST "/patients" for 127.0.0.1 at 2015-09-02 14:56:38 -0700
Processing by PatientsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXX=", "patient"=>{"user_attributes"=>{"email"=>"allencentre", "password"=>"[FILTERED]"}, "first_name"=>"Allen", "last_name"=>"Centre", "birth_date(1i)"=>"2002", "birth_date(2i)"=>"9", "birth_date(3i)"=>"2", "clinician_ids"=>["85", "87"], "patient_deceased"=>"0", "patient_archived"=>"0"}, "button"=>""}
So "clinician_ids"=>["85", "87"]
is being passed from the form.
There is an INSERT INTO "users" ("email", ....
and an INSERT INTO "patients" ("bir....
but the data for care_group_assignments
doesn't have one.
There is [36mCareGroupAssignment Load (0.4ms)[0m [1mSELECT "care_group_assignments".* FROM "care_group_assignments" WHERE "care_group_assignments"."patient_id" = $1[0m [["patient_id", 199]]
clinician.rb (simplified)
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
This is following 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).
patient new.html.erb form
<%= 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 method has so far not 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,:birth_date, user_attributes: [ :email, :password, :patient_id, :clinician_id ])
end
I plan to display the clinicians
associated with a patient
on the patient show
page:
patient 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
Thanks
回答1:
I believe your problem is this line in your controller:
params.require(:patient).permit({:clinician_ids => [:id]}...
It should be:
params.require(:patient).permit({:clinician_ids => []}...
来源:https://stackoverflow.com/questions/32364030/has-many-throught-not-instering-into-database-from-form