2015-12-02 83 views
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,但是当我提交表单活动记录不保存其他参数病人像namelast_nameageblood_type

这是我的模型/ medic.rb(设计 - 用户)

class Medic < ActiveRecord::Base 
    has_many :patients 
end 

这是我的模型/ patient.rb

class Patient < ActiveRecord::Base 
    belongs_to :medic 
end 

我需要一种方法来从medic-user呼吁所有寄存器(患者),到show图。什么是最好的方式来做到这一点?

感谢您的帮助。

+1

尝试current_user.patients.build(patient_params.merge({:medic_id => current_user.id})) –

+0

谢谢,它的工作!我感谢您的帮助 :) –

回答

0

要当您创建一个病人解决问题:

medic_id = {medic_id: current_user.id} 
current_user.patients.build(patient_params.merge(medic_id)) 

假设你有@medicshow行动,得到的medic-user所有患者:

@patients = @medic.patients 

而在你show视图,你可以作为一个数组访问@patients