처음부터 R Bates 인증을 here에서 롤링하고 있습니다. 응용 프로그램 컨트롤러에서 authorize 메소드를 호출하고 싶습니다. 기본적으로 전체 앱을 잠그고 싶습니다. 다음은 앱 컨트롤러입니다 ...R Bates authentication with application controller
class ApplicationController < ActionController::Base
before_filter :authorize
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
def authorize
redirect_to login_url, alert: "Not authorized" if current_user.nil?
end
end
하지만 아마도 내 URL 호출에 무한 루프가 발생합니다. 어떻게해야합니까?
세션 제어기
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, notice: "Logged in!"
else
flash.now.alert = "Email or password is invalid"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, notice: "Logged out!"
end
end
가장 예쁜 것은 아니지만 작동합니다! – Lumbee
네가 맞다. @Lumbee, 가장 예쁜 것은 아니다. 방금 skip_before_filter 접근법을 언급 한 답변도 편집했습니다. – markets
@markets가 맞지만 건너 뛰기 필터가 작동하지 않습니다. – Lumbee