我一直收到DoubleRenderError,我无法弄明白为什么!基本上,我有一个动作,调用另一个动作,检查用户输入的查询错误,如果有错误,它停止并显示错误。但是当我输入一个带有错误的查询时,当我得到双重渲染时!有什么建议吗?
继承错误检查器操作:
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
以防您需要调用此操作的操作..
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
更新
也忘了提一下,这最初是一个rails 2应用程序并且正在工作,当我升级到rails 3(我相信)时这个错误开始了,所以也许rails 3与and return
做了不同的事情?
答案 0 :(得分:7)
您只是从if_user_formulated_request_properly
方法返回,这意味着redirect_to
和respond_to
都会进行渲染。
你可以试试这个:
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
答案 1 :(得分:1)
这是我的解决方案:
“根本原因是在触发错误之前分配了response_body的某些部分。
您可以在异常处理程序中调用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
来源: DoubleRenderError in Rails 4.1 when rescuing from InvalidCrossOriginRequest