制定行动过滤器需要身份验证的操作 [英] Devise action filter for actions that require authentication

查看:130
本文介绍了制定行动过滤器需要身份验证的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的设计进行验证,但我不能看到和指定要求用户登录的操作行为过滤器,这是包含在色器件宝​​石?如果不是我怎么能创建一个,我种有一个想法,但因为我是新来的护栏,我宁愿看到一个更有经验的程序员第一个解决方案。

I'm using devise for authentication, however I cant see and action filter for specifying actions that require the user to login, is this included in the devise gem? if not how could I create one, I kind of have an idea but since I'm new to rails I'd rather see a solution from a more experienced programmer first.

推荐答案

查看设计自述

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

这篇关于制定行动过滤器需要身份验证的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆