LoadError: Unable to autoload constant ApplicationController
LoadError: Unable to autoload constant ApplicationController
我对以下 LoadError 感到有些困惑。
LoadError - Unable to autoload constant ApplicationController, expected /opt/rails/production/current/app/controllers/application_controller.rb to define it:
activesupport (4.2.4) lib/active_support/dependencies.rb:495:in `load_missing_constant'
activesupport (4.2.4) lib/active_support/dependencies.rb:184:in `const_missing'
app/controllers/display_controller.rb:3:in `<top (required)>'
activesupport (4.2.4) lib/active_support/dependencies.rb:457:in `block in load_file'
activesupport (4.2.4) lib/active_support/dependencies.rb:647:in `new_constants_in'
activesupport (4.2.4) lib/active_support/dependencies.rb:456:in `load_file'
activesupport (4.2.4) lib/active_support/dependencies.rb:354:in `require_or_load'
activesupport (4.2.4) lib/active_support/dependencies.rb:494:in `load_missing_constant'
activesupport (4.2.4) lib/active_support/dependencies.rb:184:in `const_missing'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:261:in `block in constantize'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:259:in `constantize'
activesupport (4.2.4) lib/active_support/dependencies.rb:566:in `get'
activesupport (4.2.4) lib/active_support/dependencies.rb:597:in `constantize'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:72:in `controller_reference'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:62:in `controller'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:41:in `serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
bullet (4.14.7) lib/bullet/rack.rb:12:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.4) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.4) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
rack-contrib (1.4.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/headers.rb:16:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.4) lib/rails/rack/logger.rb:22:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.4) lib/rails/engine.rb:518:in `call'
railties (4.2.4) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/home/production/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/production/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/production/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
我已经将我的控制器限制为一个方法,它仍然这样做,所以它与控制器中的代码无关。
这是它的路线:
namespace :display do
get :delivery
end
以下是开发选项:
config.cache_classes = false
config.eager_load = false
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
config.assets.js_compressor = :uglifier
config.serve_static_files = true
config.action_dispatch.cookies_serializer = :hybrid
config.active_record.raise_in_transactional_callbacks = true
这是我的 ApplicationController
class ApplicationController < ActionController::Base
include AuthenticatedSystem
helper :all
unloadable
protect_from_forgery
rescue_from ActionController::InvalidAuthenticityToken, :with => :bad_token
def fail
raise "Well, that was exciting."
end
private
def bad_token
flash[:notice] = "Your session has expired."
redirect_to :controller => :account, :action => :login
end
def authorize_edit
authorized! "Edit " + @project.data_type
end
def authorize_view
authorized! "View " + @project.data_type
end
def set_project
@project = Project.find_by(id: params[:project_id])
end
end
这种情况发生在各种控制器上,并且 始终如一 在我更改代码之后。更改发生在哪里似乎并不重要(例如控制器或模型)。我唯一的补救办法是每次更改代码后都重新启动服务器——非常令人沮丧。
其他与此相关的 SO 帖子建议将控制器复数化,但这里复数控制器没有意义,除此之外,我已经尝试过了,但它不起作用。此外,自动加载器只需要文件名与 module/class 名称匹配,对吗?
我的问题是,根据堆栈跟踪中的 LoadError 消息,我不确定要修复什么。
这似乎与主动支持处理自动加载的方式有关,我很想从 Rails 4.2.4 升级到 4.2.5,但我不相信这会解决这个问题。
如何避免此 LoadError?
在 ApplicationController
中使用 unloadable
导致控制器 class 被卸载,并且在开发环境发生变化后显然无法再次正确自动加载。
app/
中的所有内容默认情况下已经可以卸载,不需要显式调用。
有关详细信息,请查看 this issue in the Rails repository on Github。
我对以下 LoadError 感到有些困惑。
LoadError - Unable to autoload constant ApplicationController, expected /opt/rails/production/current/app/controllers/application_controller.rb to define it:
activesupport (4.2.4) lib/active_support/dependencies.rb:495:in `load_missing_constant'
activesupport (4.2.4) lib/active_support/dependencies.rb:184:in `const_missing'
app/controllers/display_controller.rb:3:in `<top (required)>'
activesupport (4.2.4) lib/active_support/dependencies.rb:457:in `block in load_file'
activesupport (4.2.4) lib/active_support/dependencies.rb:647:in `new_constants_in'
activesupport (4.2.4) lib/active_support/dependencies.rb:456:in `load_file'
activesupport (4.2.4) lib/active_support/dependencies.rb:354:in `require_or_load'
activesupport (4.2.4) lib/active_support/dependencies.rb:494:in `load_missing_constant'
activesupport (4.2.4) lib/active_support/dependencies.rb:184:in `const_missing'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:261:in `block in constantize'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:259:in `constantize'
activesupport (4.2.4) lib/active_support/dependencies.rb:566:in `get'
activesupport (4.2.4) lib/active_support/dependencies.rb:597:in `constantize'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:72:in `controller_reference'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:62:in `controller'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:41:in `serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
bullet (4.14.7) lib/bullet/rack.rb:12:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.4) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.4) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
rack-contrib (1.4.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/headers.rb:16:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.4) lib/rails/rack/logger.rb:22:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.4) lib/rails/engine.rb:518:in `call'
railties (4.2.4) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/home/production/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/production/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/production/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
我已经将我的控制器限制为一个方法,它仍然这样做,所以它与控制器中的代码无关。
这是它的路线:
namespace :display do
get :delivery
end
以下是开发选项:
config.cache_classes = false
config.eager_load = false
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
config.assets.js_compressor = :uglifier
config.serve_static_files = true
config.action_dispatch.cookies_serializer = :hybrid
config.active_record.raise_in_transactional_callbacks = true
这是我的 ApplicationController
class ApplicationController < ActionController::Base
include AuthenticatedSystem
helper :all
unloadable
protect_from_forgery
rescue_from ActionController::InvalidAuthenticityToken, :with => :bad_token
def fail
raise "Well, that was exciting."
end
private
def bad_token
flash[:notice] = "Your session has expired."
redirect_to :controller => :account, :action => :login
end
def authorize_edit
authorized! "Edit " + @project.data_type
end
def authorize_view
authorized! "View " + @project.data_type
end
def set_project
@project = Project.find_by(id: params[:project_id])
end
end
这种情况发生在各种控制器上,并且 始终如一 在我更改代码之后。更改发生在哪里似乎并不重要(例如控制器或模型)。我唯一的补救办法是每次更改代码后都重新启动服务器——非常令人沮丧。
其他与此相关的 SO 帖子建议将控制器复数化,但这里复数控制器没有意义,除此之外,我已经尝试过了,但它不起作用。此外,自动加载器只需要文件名与 module/class 名称匹配,对吗?
我的问题是,根据堆栈跟踪中的 LoadError 消息,我不确定要修复什么。
这似乎与主动支持处理自动加载的方式有关,我很想从 Rails 4.2.4 升级到 4.2.5,但我不相信这会解决这个问题。
如何避免此 LoadError?
在 ApplicationController
中使用 unloadable
导致控制器 class 被卸载,并且在开发环境发生变化后显然无法再次正确自动加载。
app/
中的所有内容默认情况下已经可以卸载,不需要显式调用。
有关详细信息,请查看 this issue in the Rails repository on Github。