嗨,我想发布xml数据到服务器,并根据响应重定向页面。为此,在我的控制器,我有我的动作和send_xml方法。
Ruby版本:2.0.0-p 247
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方法会触发呈现或重定向,从而导致此错误。
我怎样才能摆脱这个错误。
谢谢你的帮助
发布于 2014-02-24 13:52:07
这是因为您正在从render xml: request调用#send_xml中调用render和redirect,因此调用了render和redirect,这将导致异常(正如消息所指出的那样)。
https://stackoverflow.com/questions/21989677
复制相似问题