Devise action filter for actions that require authentication

眉间皱痕 提交于 2019-12-21 07:50:41

问题


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.


回答1:


See the 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



回答2:


The other solution is to use for example :except => login, its using when the entire app use authentication and you want to have a page with public access



来源:https://stackoverflow.com/questions/4769402/devise-action-filter-for-actions-that-require-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!