2015-12-02 84 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