2011-01-22 57 views
11

我正在使用devise進行認證,但是我無法看到並指定需要用戶登錄的動作過濾器,這是否包含在設計寶石中?如果不是我怎麼能創建一個,我有種想法,但由於我是新手,我寧願首先看到一個更有經驗的程序員的解決方案。爲需要認證的動作設計動作過濾器

回答

23

請參閱Devise Readme

class PostsController < ApplicationController 
    respond_to :html 

    # Tell Devise that the #destroy action is 
    # special, and that the user must be 
    # authenticated in order to access the 
    # #desroy action. 
    # Note that the name of the method here, 
    # #authenticate_user!, depends on the 
    # particular class/table that you have 
    # set up to be managed with Devise. 
    before_filter :authenticate_user!, 
    :only => [:destroy] 

    before_filter :find_post!, 
    :only => [:destroy] 

    def destroy 
    @post.destroy 
    respond_with @post 
    end 

    private 

    def find_post! 
    @post = Post.find(params[:id]) 
    end 
end 
0

另一個解決方案是使用例如:除了=>登錄,其使用時,整個應用程序使用的身份驗證和你想有一個頁面,其中公共接入