Eric Baldwin
Eric Baldwin

Reputation: 3501

User Controller not correctly applying devise's before_filter authenticate_user

I have explicitly called a before_filter to authenticate users before every controller action.

class ApplicationController < ActionController::Base
  before_filter :authenticate_user!

class UsersController < Devise::RegistrationsController
    before_filter :authenticate_user!

If, when I am logged out, I attempt to access any page, I am redirected to the sign_in page UNLESS that page is rendered by the UsersController.

If that happens, I am granted access to that page UNLESS I'm accessing the edit action.

I don't know why authenticate_user! is not working in UsersController and I'm even more stumped as to why it does work if I access edit.

Below is edit in UsersController (and another method)

class UsersController < Devise::RegistrationsController
  #redirects queries as expected
  def edit
      @user = User.find_by_id(params[:id])
  end

  #Does not redirect queries
  def about
      @user = current_user
  end

Does anyone know how I can get the expected behavior (redirection for all queries made to any action in UsersController?

Upvotes: 0

Views: 295

Answers (1)

suddenenema
suddenenema

Reputation: 351

add your action to controllers prepend_before_filter:

class UsersController < Devise::RegistrationsController
  prepend_before_filter :authenticate_scope!, only: [:edit, :update, :destroy, :about]
  ...

Upvotes: 3

Related Questions