In my rails app all of a sudden i keep getting a double render error (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at mos ....etc...) and i could not for the life of me figure out where the double render is. It comes when the user types in a query that is not the proper format. Here is the code that checks the format and displays errors:
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 example 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 example GP07-8)
redirect_to(:action => "index") and return
end
Any suggestions?
UPDATE
Controller action code as requested:
def show
if_user_formulated_request_properly do
@input_messages = InputMessage.search_by(params[:query].strip) unless params[:query].blank?
end
respond_to do |format|
format.html #default rendering
end
end
I'll suggest a refactoring for the action code and leave it to you to refactor the rest . . .
def show
unless user_formulated_request_properly?
redirect_to(:action => "index")
return
end
respond_to do |format|
format.html
end
end
In case it's not obvious, you shouldn't have any redirect calls in user_formulated_request_properly?, and you shouldn't be calling yield either. (imho, yield is the most overused ruby language feature)
I'm making some assumptions about your code.
Assuming that if_user_formulated_request_properly is not the action of your controller, your controller action is calling this method. When you do a return, you exit if_user_formulated_request_properly, but control goes back to the code in your action method.
I think you are expecting it to return from the action method, but that's not what happens. Instead, it goes to the next line in the action.
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