gpt4 book ai didi

ruby-on-rails-3 - 初始化器更改 'alias_method' 后,类 'current_user' 的未定义方法 'ApplicationController'

转载 作者:行者123 更新时间:2023-12-03 18:20:01 25 4
gpt4 key购买 nike

我有以下初始化程序:

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

我的 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

加上这个
配置/环境/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)

如果我删除调用,一切都会恢复正常(除了过滤器按预期运行)。我猜我以某种方式搞砸了依赖加载?

最佳答案

问题是你用了alias_methodApplicationController 中定义方法之前.要解决问题,请移动该行

alias_method :devise_current_user, :current_user

以下
def current_user
# do something
end

运行 skip_store_location时出现错误有点误导.我认为这是因为 skip_store_location加载多个 Controller ,其中一个是 ApplicationController 的子类.

关于ruby-on-rails-3 - 初始化器更改 'alias_method' 后,类 'current_user' 的未定义方法 'ApplicationController',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11709092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com