2013-01-16 1 views
0

내가 고안 사용하고 난에서 특정 페이지 if seller_disabled_paypal_permissions != nil에 사용자를 리디렉션 필요 내 application_controller.rb리디렉션 사용자 3

내 application_controller.rb에이 코드를

class ApplicationController < ActionController::Base 
before_filter :bad_sellers 
#more code 
. 
. 
. 
private 
    def bad_sellers 
    if user_signed_in? 
    redirect_to requirements_to_sell_user_path, :alert => t('.error') if current_user.seller_disabled_paypal_permissions != nil 
    return false 
    end 
    end 
end 

하지만 오류가 발생했습니다 :

오류 310 (net :: ERR_TOO_MANY_REDIRECTS) : 리디렉션이 너무 많습니다.

어떻게하면됩니까?

+0

봐 볼 (users_controllersapplication_controller의 자식) 에서. – Swards

답변

0

문제에 대한 수정 : 모든 리디렉션이 오는 곳

users_controllers.rbapplicatio_controller.rb

class ApplicationController < ActionController::Base 
protect_from_forgery 
before_filter :bad_sellers 
#code here 
#code here 
def bad_sellers 
    if user_signed_in? && !(current_user.seller_disabled_paypal_permissions.nil?) 
    redirect_to requirements_to_sell_user_path(current_user), :alert => t('.error') 
    end 
end 
end 

에 당신의 로그에서

class UsersController < ApplicationController 
skip_before_filter :bad_sellers, :only => [:requirements_to_sell] #parent filter on application_controller.rb 

def requirements_to_sell 
    #more code 
    #more code 
end 

end 
0
private 
    def bad_sellers 
    if user_signed_in? && !(current_user.seller_disabled_paypal_permissions.nil?) 
    redirect_to root_path, :alert => t('.error') 
    end 
    end 

내가 명시 적으로 그게 내 첫번째 추측 before_fiter에서 모든 값을 반환 적이 없어요.

ApplicationController에이 동작을 생성했으며 루트 경로가 ApplicationController을 상속하는 컨트롤러이기도하므로 무한 리디렉션 루프에 빠지게됩니다.

당신은 뭔가를해야합니다 :

before_filter :bad_sellers, :except => [:whatever_your_root_path_action_is] 
+0

고맙습니다.이 메서드를 호출하면 코드가 작동하지 않습니다. before_filter를 사용하여이 메서드를 호출하려고 시도했지만 위의 오류가 발생합니다. – hyperrjas

0

조건부 리디렉션이있을 때, 당신이 그렇게 레일보다 더 잘 넣어하는 방법을 잘 (다른 리디렉션을 볼 수 없습니다 return을 사용할 것). 귀하의 예에 따라

는 : 그것은 재귀 수 있습니다처럼

def bad_sellers 
    if user_signed_in? 
    if !current_user.seller_disabled_paypal_permissions.nil? 
     return redirect_to root_path, :alert => t('.error') 
    end 
    end 
end 
+0

같은 문제, 어떻게이 메서드를 호출 할 수 있습니까? – hyperrjas

+0

고맙습니다. 편집 한 후에 시도해 봤습니까? 나는 여분의 반환을 false에서 끝내었다. 그렇지 않으면 나는 무슨 일이 일어날 지 모른다. before_filter가 발사되고있어, 그렇지? 아마도 사적인 블록에서 이것을 꺼내십시오. – cpow

+0

"http://www.google.com"으로 리디렉션하면 정상적으로 작동합니다. 외부 페이지로 리디렉션 할 수있는 이유를 이해할 수 없지만 앱 내부에서 리디렉션을 시도하면 오류가 발생합니다! – hyperrjas

0

것 같습니다. root_path는 필터 전에 이것을 호출합니까? 이 경우 전화하지 않는 방법을 찾고 싶을 것입니다. 살펴보기 skip_before_filter

또한 '반환하지 않아도됩니다.'

+0

'skip_before_filter'를 실행하면'bad_sellers' 메소드가 호출되지 않습니다. 그런 다음 사용자는 응용 프로그램을 탐색 할 수 있습니다. 특정 페이지를 사용자에게 리디렉션하고 싶다면'current_user.seller_disabled_paypal_permissions! = nil'. 고맙습니다! – hyperrjas

+0

루트 페이지의 필터를 건너 뛰고 싶다고 생각합니다.리디렉션하는 페이지 인 경우 bad_sellers를 확인할 필요가 없습니다. – Swards

+0

루트 페이지가 예이며, 다른 페이지로 리디렉션해야합니다. 나는 사용자가'requirements_to_sell_user_path'로 가도록하고 싶다. 나는 그 질문을 수정했다. 감사합니다 – hyperrjas