장소 당신의 당신의 application_controller.rb
에 before_filter
및 params[:controller]
가 고안 컨트롤러의 이름인지 확인하십시오
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_filter :check_if_devise
private
def check_if_devise
if ['confirmations', 'omniauth_callbacks', 'passwords', 'registrations', 'sessions', 'unlocks'].include? params[:controller]
# logic for before_filter
end
end
end
업데이트 :
또는, 당신은 당신이 분리 before_filter
기존 유지하려는 경우 check_if_devise
논리를 사용하면 before_filter
에서 기존 함수를 제거하고 조건부 논리 check_if_devise
이 통과하는 경우에만 호출 할 수 있습니다.
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_filter :check_if_devise
private
def check_if_devise
if ['confirmations', 'omniauth_callbacks', 'passwords', 'registrations', 'sessions', 'unlocks'].include? params[:controller]
function_to_run
end
end
def function_to_run
# code goes here
end
end
before_filter
가 고안 컨트롤러에 대해 실행되어 있는지 확인합니다 application_controller.rb
이내에 배치. 다른 개별 일회성 컨트롤러를 들어, 호출 할 수 function_to_run
before_filter
A와 :
# app/controller/random_controller.rb
class RandomController < ApplicationController
before_filter :function_to_run
end
before_filter 논리가 이미 분리하여 정의하고, 그래서 "before_filter에 대한 논리"에 로직을 추가하는 다른 컨트롤러에서 공유 반복적 보일 것입니다 그. 이전 필터를 분리하여 보관하는 방법은 없습니까? – Martin
'check_if_devise'조건 내에서 로직을 정의하는 대신, 그 로직을 포함하는 함수를 정의한 다음, 적당하다고 생각 되더라도 호출 할 수 있습니다. 내 업데이트를 참조하십시오. – zeantsoi
작동하지 않는 것 같습니다. 계정 정보 페이지를 방문 할 때 '정의되지 않은 지역 변수 또는 메소드'current_site''를 얻습니다. current_site는'check_if_devise' 메소드 내에서 호출되는'helper_method'입니다. – Martin