简体   繁体   中英

Passing variable to the render in Rails controller method

I am doing Rails 4 app with nested forms. I have form with nested fields. Creating works fine:

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

The problem is in else condition. Firstly In my form I have @patient variable defined, so when I render new_medical_history @patient variable becomes nil.

I need to pass it back again. How can I do that?

You need to define @patient again, in preparation for the render.

So for example:

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

or whatever you need to get patient assigned again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM