I am trying to add a before_destroy filter to Devise’s SessionsController.
Here is what I have attempted:
I created a module containing the before_destroy filter and another module with the method that I wanted it to call.
module UserTracker
prepend_before_filter :stop_tracking, :only => [:destroy]
module TrackerStopper
def stop_tracking
# Do stuff
end
end
include TrackerStopper
end
And then I tried to extend the SessionsController with the UserTracker module.
Devise::SessionsController.extend UserTracker
This was not working, because devise was not loaded when my module was loaded. This resulted in the error below:
uninitialized constant Devise::SessionsController (NameError)
Basically, I would like to run a method each time a session gets destroyed, without overwriting the entire SessionsController, but I can’t figure out a way to mix it in.
Thanks!
UPDATE 1: I gave up on building a separate gem, here is the code I have attempted in my application.
UserTrackersController.rb
class UserTrackersController < Devise::SessionsController
prepend_before_filter :stop_tracking, :only => [:destroy]
def stop_tracking
current_user.update_attributes(:current_sign_in_ip => nil)
end
end
routes.rb
#--
# Devise
devise_for :users, :controllers => { :sessions => 'user_trackers'}
The problem now is that if I try to log in (or log out in this case), I get the following error:
Template is missing
Missing template user_trackers/new with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/net/user10/ardavis2/rubydev/spacecamp/app/views", "/net/user10/ardavis2/.rvm/gems/ruby-1.9.2-p180@spacecamp/gems/devise-1.3.4/app/views"
I don’t want my new UserTrackersController to do anything except add some small code to the existing Sessions controller. So I still need devise to function as it did before.
Thanks again.
Why don’t you really extend SessionsController?
E.g.
And in the routing config, you can tell devise_for which SessionsController to use:
Edit: you can generate devise views by invoking:
This will generate
app/views/devise/sessions/new.html.erbwhich is the only view you need for this to work. Just copy it toapp/views/my_sessions/new.html.erband it will work.