2016-02-01 138 views
0

我正在使用嵌套窗體來實現Rails 4應用程序。我有嵌套字段的形式。創建工作正常:將變量傳遞給Rails控制器方法中的渲染

def create_medical_history 
    @patient_id = params[:patient] 
    @medical_history = MedicalHistory.new(medical_history_params) 
    if @medical_history.save 
     redirect_to patient_path(@patient_id) 
    else 
     render 'new_medical_history' 
    end 
end 

問題是在其他條件。首先在我的表單中我定義了@patient變量,所以當我的render new_medical_history @patient變量變爲零時。

我需要再次通過它。我怎樣才能做到這一點?

+0

你可以在渲染髮生之前製作一個全局變量來保存該值嗎? – Rana

+0

您想在新的請求中再次獲得'@ patient':即'@patient = Patient.find(@patient_id)'?確保查看此用戶的用戶可以看到此內容(即使用CanCan或Pundit)。 – Carpetsmoker

回答

1

您需要再次定義@patient以準備渲染。

因此,例如:

else 
    @patient = Patient.find_by id: @patient_id 
    render 'new_medical_history' 
end 

或任何你需要得到重新分配的病人。

相關問題