Can't get skip_before_filter to work with Acti

2019-08-04 01:33发布

问题:

I am using the devise gem for authentication and have the following before_filter in my ApplicationController:

  before_filter :require_login

  def require_login
    unless user_signed_in? || params[:controller] == 'devise/sessions'
      flash[:error] = "You must be logged in to access that page."
      redirect_to new_user_session_path
    end
  end

I have recently implemented the ActiveAdmin gem and am trying to get skip_before_filter to work for ActiveAdmin, so that I can access ActiveAdmin. I have attempted the methods outlined in this post, adding the following to config/initializers/active_admin.rb:

  config.skip_before_filter :require_login

and also adding the following to one of my admin model files, listing.rb:

ActiveAdmin.register Listing do

  controller do
    skip_before_filter :require_login
  end

end

but it doesn't seem to work, even after restarting the server and browser.

What am I doing wrong?

回答1:

So two things:

First: I think you're using Devise inefficiently. Instead of a custom login method on application_controller, you should:

class ApplicationController 
  before_filter :authenticate_user!
end

That's it -- everything else works.

Second: Active Admin uses a different user class -- the "admin_user" (rather than "user"). By default, you'll use the following credentials:

email: admin@example.com 
password: password