我有以下初始化程序:
应用/配置/初始化/ 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
我的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
加上这个 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,我收到以下错误:
/foo/app/controllers/application_controller.rb:7:in `alias_method': undefined method `current_user' for class `ApplicationController' (NameError)
如果我删除了呼叫,一切都恢复正常(除了按预期运行过滤器)。我猜我以某种方式搞乱了依赖加载?
答案 0 :(得分:16)
问题是您在alias_method
中定义方法之前使用ApplicationController
。要解决此问题,请移动
alias_method :devise_current_user, :current_user
以下
def current_user
# do something
end
运行skip_store_location
时出现错误有点误导。我认为这是因为skip_store_location
加载了几个控制器,其中一个是ApplicationController
的子类。