htkaya
htkaya

Reputation: 245

DoubleRenderError after Net::HTTP.post_form

Hi i want to post xml data to server and redirect page according to response. For this in my controller i have my action and send_xml method.

Ruby Version: 2.0.0-p247

Rails Version: 3.2.17

def new
  @sales = current_user.sales.new
  respond_with(@sales)
end

def fail
  flash[:error] = 'Canceled'
  render :new
end

def success
  result = send_xml(params)
  if result['Response'] == 'Approved'
    flash[:success] = 'Approved'
    redirect_to(approved_path)
    return
  else
    flash[:error] = 'Failed'
    redirect_to(failed_path)
    return
  end
end
private
def send_xml(params)
  request = "DATA=<?xml version=\"1.0\" encoding=\"ISO-8859-9\"?><myData>Foo</myData>"
  uri = URI.parse('http://foobar.com')
  xml = render xml: request
  response = Net::HTTP.post_form(uri, xml)
  response = Hash.from_xml(response.body)
  response['myResponse']
end

When i try to run this action i get error like:

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". 

I guess NET::HTTP.post_form method triggers render or redirect and that cause this error.

How can i get rid of this error.

Thanks for help

Upvotes: 1

Views: 138

Answers (1)

Agis
Agis

Reputation: 33636

This happens because you are calling render xml: request in #send_xml which you are calling from #success, therefore you are calling render and redirect which results in the exception (as the message also indicates).

Upvotes: 1

Related Questions