2010-05-09 3 views

답변

54

레일이 내장되어 이에 대한 도우미, 당신은 당신의 응용 프로그램 컨트롤러에이를 배치 할 수 있습니다 :

protected 
    def authenticate 
    authenticate_or_request_with_http_basic do |username, password| 
     username == "admin" && password == "test" 
    end 
    end 

그런 다음 응용 프로그램 컨트롤러에 스틱 당신은 단지 보호 (또는하려는 컨트롤러에 before_filter를 사용 전체 사이트를 차단) :

before_filter :authenticate 

이 방법은 Nginx와 Apache에 추가로 적용되는 보너스입니다. 그러나 전체 페이지 캐싱을 사용하도록 설정하면 방문자가 Rails 스택에 절대 도달하지 않으므로 작동하지 않습니다.

편집 /admin 경로를 지정한 것으로 나타났습니다. 모든 관리 컨트롤러는 AdminController에서 상속받습니다. 당신과 같이 당신을 설정할 수 있습니다 :

class Admin::ThingsController < Admin::AdminController 

내 경로 :

class Admin::AdminController < ApplicationController 
    before_filter :authenticate 
    protected 
    def authenticate 
     authenticate_or_request_with_http_basic do |username, password| 
     username == "admin" && password == "test" 
    end 
    end 
end 

그런 다음 모든 컨트롤러가 관리 컨트롤러, 예를 확장해야

/app/controllers/admin/admin_controller.rb 다음과 같이 설정됩니다.

map.namespace :admin do |admin| 
    admin.resources :things 
end 

희망이 있습니다.

+3

굉장 .. 청초하고 깨끗한 설명을위한 +1 ... –

+0

이것은 내가 찾고있는 것입니다. 고맙습니다! –

+0

아주 좋은 설명! – blackbiron

관련 문제