2013-05-20 100 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等。如何在Rails中縮短控制器操作

我想索引/顯示/新只爲current_user。這是可行的,但我必須手動更改所有控制器操作。 而且我所有的自動化規格文件都只是測試@customer = customers

這似乎不是手動更換所有這些的方法。

有沒有更好的解決方案呢?

在此先感謝 最誠摯的問候 denym

+0

你能澄清你到底在問什麼嗎? – drhenner

回答

0

我認爲你正在尋找一個before_filter。例如:

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

def valid_user 
    redirect_to root_path if current_user.nil? 
end 
相關問題