I'm trying to associate a current_user.id
to a @patient.medic_id
via a controller.
I try this in my controller:
def create
# @patient = Patient.new(patient_params)
@patient = current_user.patients.build(patient_params[:medic_id => current_user.id])
respond_to do |format|
if @patient.save
format.html { redirect_to @patient, notice: 'Patient was successfully created.' }
format.json { render :show, status: :created, location: @patient }
else
format.html { render :new }
format.json { render json: @patient.errors, status: :unprocessable_entity }
end
end
end
This will automatically fill medic_id
in the view with the current_user.id
, but when I submit the form active record doesn't save the others parameters for patient like name
, last_name
, age
, blood_type
, etc.
This is my model / medic.rb
(devise - user)
class Medic < ActiveRecord::Base
has_many :patients
end
This is my model / patient.rb
class Patient < ActiveRecord::Base
belongs_to :medic
end
And I need a way to call all registers (patients) from a medic-user
, to a show
view; what it is the best way to do this?
Thanks for the help.
To solve the problem when you create a patient:
medic_id = {medic_id: current_user.id}
current_user.patients.build(patient_params.merge(medic_id))
Assuming you have @medic
in your show
action, to get all patients of a medic-user
:
@patients = @medic.patients
And in your show
view, you can access @patients
as an array.
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.