Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 941
Jonah Katz Avatar asked Aug 31 '11 16:08

Jonah Katz


People also ask

What is difference between render and redirect?

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.

How do I redirect in rails?

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.

Does redirect_to return?

redirect_to and render do not returnThe unless condition is true , so the first redirect_to is skipped. The article is destroyed.

Does redirect to stop execution?

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".


2 Answers

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
like image 81
Benoit Garret Avatar answered Nov 12 '22 06:11

Benoit Garret


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

like image 26
Hiep Dinh Avatar answered Nov 12 '22 05:11

Hiep Dinh