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
?
Render tells Rails which view or asset to show a user, without losing access to any variables defined in the controller action. Redirect is different. The redirect_to method tells your browser to send a request to another URL.
Rails's redirect_to takes two parameters, option and response_status (optional). It redirects the browser to the target specified in options. This parameter can be: Hash - The URL will be generated by calling url_for with the options.
redirect_to and render do not returnThe unless condition is true , so the first redirect_to is skipped. The article is destroyed.
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".
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With