如何调用完全合格的Devise sign_in方法?

时间:2014-06-03 00:07:08

标签: ruby-on-rails ruby-on-rails-3 devise

我如何明确地调用Devise sign_in?我的控制器中已经有一个名为sign_in的方法。

我试过

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

cannot load such file -- devise/controllers/sign_in_out

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

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

但都没有奏效。

我也试过

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

在浏览器中有效,但当我运行rspec时出现了这个错误:

  

/rsync/BK-Development/app/controllers/community_members_controller.rb:5:in alias_method': undefined method sign_in'课程CommunityMembersController' (NameError) from /rsync/BK-Development/app/controllers/community_members_controller.rb:5:in'

我在这里找到了方法的来源:

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

设计版本2.2.8

1 个答案:

答案 0 :(得分:1)

好的,我得到它与

一起工作
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

我仍然想知道如何直接调用方法。在Java中,您可以通过指定完整的包和方法来调用任何方法。应该有办法在Ruby中做到这一点。