Ruby-on-rails 在此操作中多次调用渲染和/或重定向。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7260130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Render and/or redirect were called multiple times in this action..?
提问by Jonah Katz
I keep getting the DoubleRenderError and I cannot figure out why! Basically, I have an action that calls another action that checks a user inputted query for errors, and if theres an error, its stops and displays the error. But when i type in a query with an error, that when i get the double render! Any suggstions?
我不断收到 DoubleRenderError,但我不知道为什么!基本上,我有一个动作调用另一个动作来检查用户输入的查询是否有错误,如果有错误,它会停止并显示错误。但是当我输入一个错误的查询时,当我得到双重渲染时!有什么建议吗?
Heres the error checker action:
这是错误检查器操作:
def if_user_formulated_request_properly
unless request.post?
flash[:error] = "This page can only be accessed through the search page. (POST request only)"
redirect_to(:action => "index") and return
end
if params[:query].blank?
flash[:error] = "Search criteria can not be blank"
redirect_to(:action => "index") and return
end
if !(params[:query] =~ /-/)
flash[:error] = "( Format of search criteria is wrong.<br /> Should be [IXLSpecClass value][year]-[Message ID] for exam
ple GP07-8)"
redirect_to(:action => "index") and return
end
if !(QueryParser.expression.match(params[:query]))
flash[:error] = %( Format of search criteria is wrong.<br />Should be [IXLSpecClass value][year]-[Message ID] for examp
le GP07-8)
redirect_to(:action => "index") and return
end
yield
And just in case you need the action calling this action..
以防万一您需要调用此操作的操作..
def show
if_user_formulated_request_properly do
@statuses = IXLStatus.find(:all)
@input_messages = InputMessage.search_by(params[:query].stri
p) unless params[:query].blank?
@query = params[:query]
end
respond_to do |format|
format.html #default rendering
end
end
end
UPDATE
更新
Also forgot to mention, this originally was a rails 2 app and was working, this error started when i upgraded to rails 3 (i believe), so maybe rails 3 does something different with and return?
还忘了提及,这最初是一个 rails 2 应用程序并且正在运行,当我升级到 rails 3 时开始出现此错误(我相信),所以也许 rails 3 与and return?
回答by Benoit Garret
You're only returning from the if_user_formulated_request_properlymethod, which means both the redirect_toand the respond_todo a render.
您只是从该if_user_formulated_request_properly方法返回,这意味着 theredirect_to和 therespond_to都进行渲染。
You could try this:
你可以试试这个:
def user_formulated_request_properly?
unless request.post?
flash[:error] = "This page can only be accessed through the search page. (POST request only)"
return false
end
if params[:query].blank?
flash[:error] = "Search criteria can not be blank"
return false
end
if !(params[:query] =~ /-/)
flash[:error] = "( Format of search criteria is wrong.<br /> Should be [IXLSpecClass value][year]-[Message ID] for example GP07-8)"
return false
end
if !(QueryParser.expression.match(params[:query]))
flash[:error] = %( Format of search criteria is wrong.<br />Should be [IXLSpecClass value][year]-[Message ID] for example GP07-8)
return false
end
return true
end
def show
if user_formulated_request_properly?
@statuses = IXLStatus.find(:all)
@input_messages = InputMessage.search_by(params[:query].strip) unless params[:query].blank?
@query = params[:query]
else
redirect_to(:action => "index") and return
end
respond_to do |format|
format.html #default rendering
end
end
回答by Hiep Dinh
This my solution for some case:
这是我针对某些情况的解决方案:
"The underlying reason is that some part of the response_body is assigned before the error is triggered.
“根本原因是在触发错误之前分配了 response_body 的某些部分。
You could try clearing the response body before calling render in the exception handler."
您可以尝试在异常处理程序中调用 render 之前清除响应正文。”
def render_400
# Clear the previous response body to avoid a DoubleRenderError
# when redirecting or rendering another view
self.response_body = nil
render(nothing: true, status: 400)
end
Source: DoubleRenderError in Rails 4.1 when rescuing from InvalidCrossOriginRequest
来源: 从 InvalidCrossOriginRequest 救援时 Rails 4.1 中的 DoubleRenderError

