在“ApplicationController”类之后,初始化程序更改“alias_method”undefined方法“current_user” [英] After initializer change 'alias_method' undefined method 'current_user' for class 'ApplicationController'

查看:185
本文介绍了在“ApplicationController”类之后,初始化程序更改“alias_method”undefined方法“current_user”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下初始化器:



app / config / initializers / store_location.rb

 模块StoreLocation 

def self.skip_store_location
[
Devise :: SessionsController,
设计: :RegistrationsController,
Devise :: PasswordsController
] .each do | controller |
controller.skip_before_filter:store_location
end
end

self.skip_store_location
end

我的ApplicationController的相关部分:

  class ApplicationController< ActionController :: Base 
protect_from_forgery
before_filter:convert_legacy_cookies
before_filter:store_location

alias_method:devise_current_user,:current_user

def current_user
#做某事
end

private
def store_location
#store location
end


config / environments / development.rb

  Foo :: Application.configure do 
#normal rails stuff
config.to_prepare do
StoreLocation.skip_store_location
end
end

如果我让RSpec / Rails运行self.skip_store_location,我得到以下错误: p>

  /foo/app/controllers/application_controller.rb:7:in`alias_method':对于ApplicationController类的未定义方法`current_user' (NameError)

如果我删除该通话,一切都回到正常(除了过滤器运行,如预期)。我猜,我搞砸了依赖加载吗?

解决方案

问题是你使用 alias_method在 ApplicationController 中定义方法之前。要解决问题,请移动

  alias_method:devise_current_user,:current_user 
/ pre>

以下

  def current_user 

end

运行 skip_store_location 。我认为这是因为 skip_store_location 加载了几个控制器,其中一个是 ApplicationController 的子类。


I've got the following initializer:

app/config/initializers/store_location.rb

module StoreLocation

  def self.skip_store_location
    [
        Devise::SessionsController,
        Devise::RegistrationsController,
        Devise::PasswordsController
    ].each do |controller|
      controller.skip_before_filter :store_location
    end
  end

  self.skip_store_location
end

Relevant parts of my ApplicationController:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :convert_legacy_cookies
  before_filter :store_location

  alias_method :devise_current_user, :current_user

  def current_user
    # do something
  end

  private
  def store_location
    # store location
  end

Plus this in config/environments/development.rb

Foo::Application.configure do
# normal rails stuff
config.to_prepare do
    StoreLocation.skip_store_location
  end
end

If I let RSpec/Rails run the self.skip_store_location I'm getting the following error:

/foo/app/controllers/application_controller.rb:7:in `alias_method': undefined method `current_user' for class `ApplicationController' (NameError)

If I remove the call, everything is back to normal (except the filter is run, as expected). I'm guessing that I mess up dependency loading somehow?

解决方案

The problem is that you use alias_method before the method is defined in ApplicationController. To fix the problem, move the line

alias_method :devise_current_user, :current_user

below

def current_user
  # do something
end

It's a bit misleading that the error appears when running skip_store_location. I assume that happens because skip_store_location loads several controllers, and one of them is a subclass of ApplicationController.

这篇关于在“ApplicationController”类之后,初始化程序更改“alias_method”undefined方法“current_user”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆