ryudice
ryudice

Reputation: 37426

Devise action filter for actions that require authentication

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.

Upvotes: 11

Views: 11304

Answers (2)

jacr1614
jacr1614

Reputation: 1310

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

Upvotes: 0

yfeldblum
yfeldblum

Reputation: 65445

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

Upvotes: 23

Related Questions