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})) –
谢谢,它的工作!我感谢您的帮助 :) –