2011-08-31 5 views
3

DoubleRenderError가 계속 발생하며 이유를 파악할 수 없습니다! 기본적으로, 사용자가 입력 한 쿼리에서 오류를 검사하는 다른 동작을 호출하는 동작이 있으며 오류가 발생하면 해당 동작이 중지되고 오류가 표시됩니다. 하지만 오류가있는 쿼리를 입력하면 이중 렌더링이 실행됩니다. 어떤 욕설?이 작업에서 렌더링 및/또는 리디렉션이 여러 번 호출되었습니다 ..?

Heres는 오류 검사기 조치 :

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 

UPDATE는

또한,이 얘기를 깜빡 했네요 원래 레일 2 응용 프로그램이었고,이 오류는 내가 레일 3 (내가 믿는다)로 업그레이드했을 때 시작되었다. 그래서 아마 레일 3은 일부를 수행한다. and return와 다른 것?

답변

5

if_user_formulated_request_properly 메서드에서만 반환됩니다. 즉 redirect_torespond_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 
+0

먼저 어떤 행동 문제를합니까? 원래 주문과 변경 사항을 남겨 두었 기 때문에'undefined method '를 얻었습니다. if_user_formulated_request_properly'' –

+0

메소드의 이름을 변경하여 명확하게 만들었습니다. 이제는'user_formulated_request_properly? '가되었습니다. –

+0

자신을 바로 잡을 수 있어야합니다 :) 어쨌든 완벽한 근무 .. 감사합니다 무리 –

1

일부 경우에이 내 솔루션 :

"근본적인 이유는 오류가 트리거되기 전에 response_body의 일부가 할당된다는 점이다

. 예외 처리기에서 렌더링을 호출하기 전에 응답 본문을 지울 수 있습니다. "

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

관련 문제