为需要身份验证的操作设计操作筛选器

时间:2011-01-22 17:35:37

标签: ruby-on-rails authentication authorization devise

我正在使用设计进行身份验证,但是我无法看到和操作过滤器来指定需要用户登录的操作,这是否包含在设计宝石中?如果不是我怎么能创建一个,我有一个想法,但由于我是铁杆新手,我宁愿首先看一个更有经验的程序员的解决方案。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

另一个解决方案是使用例如:except =&gt;登录,它在整个应用程序使用身份验证时使用,并且您希望拥有一个具有公共访问权限的页面