How can I set a helper method in my app that when I call it, recognizes a current_patient from a certain medic (can be multiple medics, and multiple patients for a medic) and access to patient :id, this would help me to associate:
Medic with a Patient with a Consultations for current_patient
I need to access on a patient and set id (patient) on a Consultation table foreign key :patient_id
in my create action in the controller I have:
def create
@consultation = Consultation.new(consultation_params.merge({:patient_id => current_patient}))
respond_to ...
end
Is a helper method a good way to do this?
How can I do this?
my models:
class Medic < ActiveRecord::Base
has_many :patients
end
class Patient < ActiveRecord::Base
belongs_to :medic, :foreign_key => :medic_id
has_many :consultations
accepts_nested_attributes_for :consultations
end
class Consultation < ActiveRecord::Base
belongs_to :patient, :foreign_key => :patient_id
end
Thanks for help
Lets start with the routes:
resources :patients do
resources :consultations
end
This will give us the routes for consultations
nested under the patient:
Prefix Verb URI Pattern Controller#Action
patient_consultations GET /patients/:patient_id/consultations(.:format) consultations#index
POST /patients/:patient_id/consultations(.:format) consultations#create
new_patient_consultation GET /patients/:patient_id/consultations/new(.:format) consultations#new
edit_patient_consultation GET /patients/:patient_id/consultations/:id/edit(.:format) consultations#edit
patient_consultation GET /patients/:patient_id/consultations/:id(.:format) consultations#show
PATCH /patients/:patient_id/consultations/:id(.:format) consultations#update
PUT /patients/:patient_id/consultations/:id(.:format) consultations#update
DELETE /patients/:patient_id/consultations/:id(.:format) consultations#destroy
So lets say we have a PatientsController#index
method which shows all the patients. And in our view we have something like this:
<% @patients.each do |patient| %>
<li>
<p><%= patient.name %></p>
</li>
<% end %>
So lets add a link to create the consultation:
<% @patients.each do |patient| %>
<p><%= patient.name %></p>
<ul class="actions">
<li>
<%= link_to "New consultation",
new_patient_consultation_path(patient) %>
</li>
</ul>
<% end %>
Clicking the link would take us to /patients/6/consultations/new
.
So in our ConsultationsController
we can access the patient id from the params:
class ConsultationsController < ApplicationController
# We use a callback so that we don't need to do
# @patient = Patient.find(params[:id]) in every action
before_action :set_patient
# GET /patients/:patient_id/consultations/new
def new
@consultation = @patient.consultations.new
end
# POST /patients/:patient_id/consultations
def create
@consultation = @patient.consultations.new(consultation_params)
# ...
end
# ...
private
def set_patient
@patient = Patient.find(params[:patient_id])
end
# ...
end
set_patient
is just a private method belonging to the controller. This is not really a case where you would use a helper method.
Its often done when dealing with dealing with authentication since you are getting current_user
from the session - independently from the params. If you are creating a helper method it should work everywhere.
There is one final thing you need to to get this to work, the form needs to point to /patients/:patient_id/consultations
.
# app/views/consultations/_form.html.erb
<% form_for [@patient, @consultation] do |f| %>
# .. rails does all the magic figuring out the url.
<% end %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.