jeudi 7 septembre 2023

Ruby on Rails - Double Render Error - Redirect and Return

Error: AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".)

On update, model checks for template.exists?, if true, download the report AND redirect to parent object. Else, just redirect.

I'm using 'and return' on the tangential side of the 'if' statement, so why isn't that working as expected?

def update
  template = resource.check_changes(params[:well_master])
  paramString = resource.to_json

  update! do |format|
    if resource.errors.present?
      return render :edit
    else
      if template != ''
        generateWellLetter(template, 'Rpt_Farm_Name', paramString) 
      else
        rec = resource.PERMIT
        format.html { redirect_to admin_well_master_path(rec) }
      end
    end
  end
 end
end

def generateWellLetter(template, letterName, params)
    @URL = "#{ENV["API_HOST"]}/api/Document/Generate/#{template}"
    response = HTTParty.post(@URL,
                             :body => params,
                             :headers => { "Content-Type" => "application/json" })

Zip::InputStream.open(StringIO.new(response)) do |io|
  while entry = io.get_next_entry
    report = io.read
    send_data report, :filename => letterName + '.docx', :type => 'docx'
  end
end

respond_to do |format|
  rec = resource.PERMIT
  format.html { redirect_to admin_well_master_path(rec) } and return
end

end

Aucun commentaire:

Enregistrer un commentaire