Devise: Devise controllers should be extended of ActionController::Base not ApplicationController

Created on 19 May 2010  路  6Comments  路  Source: heartcombo/devise

I use Admin::ApplicationController class as base class for my admin side and ApplicationController class as base class for client side.
There are some different filter methods specific for admin and client sides.
That's why I add :admin namespace to separate base classes:
#app/controllers/admin/
class Admin::ApplicationController < ActionController::Base
# some special code for beckend...
end

#app/controllers/
class ApplicationController < ActionController::Base
  # some other code for frontend...
end

But all Devise controllers are extended of ApplicationController class.
I think they should be extended of ActionController::Base because it's a base for both ApplicationController and Admin::ApplicationController classes and it doesn't have any filters or namespace specific things.

Will be even better if there are some methods to include appropriate functionality directly to any controller that you want without extending them of Devise controllers.
I mean something like you have in your inherited_resources gem:
class Admin::UsersController < Admin::ApplicationController
inherit_resources
end
It migth be like:
class Admin::UsersController < Admin::ApplicationController
devise :sessions
end

Most helpful comment

@akshayrawat you can specify any controller you want now https://github.com/plataformatec/devise/blob/f5e33089a032f44abd80225e34370c4fdd651a25/lib/devise.rb#L205 :)

All 6 comments

Yes, I've been pondering about it for a long time already. But, in your case, I would have a User::ApplicationController and do not put User logic inside the ApplicationController.

The main tradeoff here is that, if Devise inherits from ActionController::Base, a few things you add to your ApplicationController, like include MemoryLogic, Oing, Rack-bug, they won't be added to your devise controller and you will have to do it manually.

Alternatively, making Devise inherit from ActionController::Base would make Devise code simpler since it would eliminate some reloading and before filter issues.

Suggestion: Make a controller called SessionsController extending Devise::SessionsController which extends ApplicationController.

If dev wants to change the behavior of SessionsController, they can create a clean controller on app/controller/sessions_controller.rb which just extend the Devise:SessionsController.

All Devise logic will go on Devise::SessionsController (or another name like that).

What you think?

I've already done like you said. Thanks for advise!

This is quite unexpected and can lead to a lot of problems.

@akshayrawat you can specify any controller you want now https://github.com/plataformatec/devise/blob/f5e33089a032f44abd80225e34370c4fdd651a25/lib/devise.rb#L205 :)

IMHO the default (expectation) should be ActionController::Base and not ApplicationController

Was this page helpful?
0 / 5 - 0 ratings