为需要身份验证的操作设计动作过滤器 [英] Devise action filter for actions that require authentication
本文介绍了为需要身份验证的操作设计动作过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用devise进行身份验证,但是我无法查看和操作过滤器来指定需要用户登录的操作,这是否包含在设计宝石中?如果不是我可以创建一个,我有一个想法,但由于我是新的rails,我宁愿从一个更有经验的程序员首先看到一个解决方案。
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屋!
查看全文