Net :: HTTP.post_form之后的DoubleRenderError

时间:2014-02-24 13:48:58

标签: ruby-on-rails ruby-on-rails-3.2

您好我想根据响应将xml数据发布到服务器并重定向页面。 为此,在我的控制器中,我有我的动作和send_xml方法。

Ruby版本:2.0.0-p247

Rails版本: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

当我尝试运行此操作时,我收到如下错误:

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

我猜.NET :: HTTP.post_form方法会触发渲染或重定向,从而导致此错误。

我怎样才能摆脱这个错误。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

之所以发生这种情况,是因为您拨打了render xml: request#send_xml的{​​{1}},因此您正在呼叫#success {{ 1}}导致异常(如消息所示)。