Render and/or redirect were called multiple times in this action..?
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?
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
?
You're only returning from the if_user_formulated_request_properly
method, which means both the redirect_to
and the respond_to
do a render.
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
This my solution for some case:
"The underlying reason is that some part of the response_body is assigned before the error is triggered.
You could try clearing the response body before calling render in the exception handler."
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
精彩评论