0
我試圖通過控制器將current_user.id
與@patient.medic_id
關聯。由rails中的控制器關聯current_user 4
我嘗試這在我的控制器:
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
這會自動填入medic_id
視圖與current_user.id
,但是當我提交表單活動記錄不保存其他參數病人像name
, last_name
,age
,blood_type
等
這是我的模型/ medic.rb
(設計 - 用戶)
class Medic < ActiveRecord::Base
has_many :patients
end
這是我的模型/ patient.rb
class Patient < ActiveRecord::Base
belongs_to :medic
end
我需要一種方法來從medic-user
呼籲所有寄存器(患者),到show
圖。什麼是最好的方式來做到這一點?
感謝您的幫助。
嘗試current_user.patients.build(patient_params.merge({:medic_id => current_user.id})) –
謝謝,它的工作!我感謝您的幫助 :) –