2011-01-22 56 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

另一个解决方案是使用例如:除了=>登录,其使用时,整个应用程序使用的身份验证和你想有一个页面,其中公共接入