Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call the fully qualified Devise sign_in method?

How do I explicitely call Devise sign_in? I already have a method in my controller called sign_in.

I tried

require 'devise/controllers/sign_in_out'
  Devise::Controllers::SignInOut.sign_in @user

cannot load such file -- devise/controllers/sign_in_out

and

include Devise::Controllers::Helpers
  Devise::Controllers::Helpers.sign_in @user

undefined method `sign_in' for Devise::Controllers::Helpers:Module

but neither worked.

I also tried

alias_method :devise_sign_in, :sign_in # keep hold of the Devise sign_in method before we overwrite it.
  devise_sign_in @user

which worked in the browser but when I ran rspec it gave this error:

/rsync/BK-Development/app/controllers/community_members_controller.rb:5:in alias_method': undefined methodsign_in' for class CommunityMembersController' (NameError) from /rsync/BK-Development/app/controllers/community_members_controller.rb:5:in'

I found the source of the method here:

https://github.com/plataformatec/devise/blob/v2.2/lib/devise/controllers/helpers.rb

Devise version 2.2.8

like image 566
Chloe Avatar asked Nov 10 '22 07:11

Chloe


1 Answers

Ok I got it to work with

include Devise::Controllers::Helpers
alias_method :devise_sign_in, :sign_in # keep hold of the Devise sign_in method before we overwrite it.
...
  devise_sign_in @user

I'd still like to know how to call a method directly. In Java, you can call any method by specifying the full package and method. There should be a way to do that in Ruby too.

like image 120
Chloe Avatar answered Nov 14 '22 21:11

Chloe