2013-05-20 2 views
1
def index 
    @customers = current_user.customers 
    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @customers } 
    end 
    end 

    def show 
    @customer = current_user.customers.find(params[:id]) 
    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @customer } 
    end 
    end 

    def new 
    @customer = current_user.customers.new 
    end 

이것은 내 CustomerController의 일부입니다. 원산지 @customers@customers = customers 등이었습니다.레일에서 컨트롤러 동작을 단축하는 방법

current_user에 대해서만/show/new를 색인 싶었습니다. 이 작동하지만 모든 컨트롤러 작업을 수동으로 변경해야했습니다. 또한 모든 자동화 된 사양 파일은 @customer = customers에 대한 테스트 중입니다.

이것은 수동으로 모든 것을 바꾸는 레일 방식이 아닙니다.

더 나은 솔루션이 있습니까? 사전에

감사 안부 denym

+0

정확히 무엇을 묻고 있습니까? – drhenner

답변

0

Responders와 조합 inherited_resources 보석을 사용하는 것입니다 해결하는 레일 방법. 유스 케이스는 Overwriting Defaults section of inherited_resources에 설명되어 있습니다.

이 접근법은 José Valims 서적 Crafting Rails Applications: Expert Practices for Everyday Rails Development의 "응답기 및 생성기가있는 DRY 컨트롤러 작성"장에서도 설명됩니다.

+0

레일 캐스트가 발견되었습니다.이 아이디어에 감사드립니다. –

0

나는 before_filter를 찾고 있다고 생각합니다. 다음과 같이 입력하십시오 :

before_filter :valid_user, only: [:index, :new, :show] 

def valid_user 
    redirect_to root_path if current_user.nil? 
end 
관련 문제