2
/app
  /controllers
    /users
      profiles_controller.rb
      users_controller.rb
  /models
    /user
      /profile.rb
    user.rb


#/app/models/user.rb
class User < ActiveRecord::Base
  has_one :profile, class_name: "User::Profile"
end

#/app/models/user/profile.rb
class User::Profile < ActiveRecord::Base
  belongs_to :user
end

#/app/controllers/users/profiles_controller.rb
class Users::ProfilesController < ApplicationController
  layout "layouts/profile"
end

#/app/controllers/users/users_controller.rb
class Users::UsersController < Users::ProfilesController
  def show
    @user = User.find(params[:id])
  end
end

在控制台中访问 Profile 模型时,我没有错误:

vagrant@lucid32:/vagrant$ rails c
加载开发环境(Rails 3.2.11)
irb(main):001:0> u = User.new
=> <#User id: nil, created_at: nil, updated_at: nil, username: nil, crypted_pa​​ssword: nil, password_salt: nil, persistence_token: nil, perishable_token: nil, activate_token: nil, login_count: 0, failed_login_count: 0, 已激活: false, 禁止: false, last_request_at: nil, last_login_at: nil, current_login_at: nil, last_login_ip: nil, current_login_ip: nil>
irb(main):002:0> u.build_profile
   (0.5ms) 开始
   (0.4ms) 提交
=> <#User::Profile id: nil, user_id: nil, person: nil, company: nil, company_position: nil, info: nil,birthday: nil, created_at: nil, updated_at: nil>

尝试访问用户/用户#show 时出现以下错误。

用户中的 LoadError::UsersController#show

预期 /vagrant/app/models/user/profile.rb 来定义 Profile

activesupport (3.2.11) lib/active_support/dependencies.rb:503:in `load_missing_constant'
activesupport (3.2.11) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.11) lib/active_support/dependencies.rb:514:in `load_missing_constant'
activesupport (3.2.11) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:230:in `block in constantize'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:229:in `each'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:229:in `constantize'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:260:in `safe_constantize'
activesupport (3.2.11) lib/active_support/core_ext/string/inflections.rb:66:in `safe_constantize'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:152:in `_default_wrap_model'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:169:in `_set_wrapper_defaults'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:133:in `inherited'
actionpack (3.2.11) lib/abstract_controller/railties/routes_helpers.rb:7:in `block (2 levels) in with'
actionpack (3.2.11) lib/action_controller/railties/paths.rb:7:in `block (2 levels) in with'
app/controllers/users/profiles_controller.rb:1:in `'
activesupport (3.2.11) lib/active_support/dependencies.rb:469:in `load'
activesupport (3.2.11) lib/active_support/dependencies.rb:469:in `block in load_file'
activesupport (3.2.11) lib/active_support/dependencies.rb:639:in `new_constants_in'
activesupport (3.2.11) lib/active_support/dependencies.rb:468:in `load_file'
activesupport (3.2.11) lib/active_support/dependencies.rb:353:in `require_or_load'
activesupport (3.2.11) lib/active_support/dependencies.rb:502:in `load_missing_constant'
activesupport (3.2.11) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `const_missing'
app/controllers/users/users_controller.rb:1:in `'
activesupport (3.2.11) lib/active_support/dependencies.rb:469:in `load'
activesupport (3.2.11) lib/active_support/dependencies.rb:469:in `block in load_file'
activesupport (3.2.11) lib/active_support/dependencies.rb:639:in `new_constants_in'
activesupport (3.2.11) lib/active_support/dependencies.rb:468:in `load_file'
activesupport (3.2.11) lib/active_support/dependencies.rb:353:in `require_or_load'
activesupport (3.2.11) lib/active_support/dependencies.rb:502:in `load_missing_constant'
activesupport (3.2.11) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.11) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:230:in `block in constantize'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:229:in `each'
activesupport (3.2.11) lib/active_support/inflector/methods.rb:229:in `constantize'
activesupport (3.2.11) lib/active_support/dependencies.rb:554:in `get'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:69:in `controller_reference'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:54:in `controller'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:32:in `call'
旅程 (1.0.4) lib/journey/router.rb:68:in `block in call'
旅程 (1.0.4) lib/journey/router.rb:56:in `each'
旅程 (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
机架 (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__1047939203__call__223494719__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
railties (3.2.11) lib/rails/railtie/configurable.rb:30:in `method_missing'
机架 (1.4.5) lib/rack/lint.rb:48:in `_call'
rack (1.4.5) lib/rack/lint.rb:36:in `call'
机架 (1.4.5) lib/rack/showexceptions.rb:24:in `call'
机架 (1.4.5) lib/rack/commonlogger.rb:33:in `call'
rack (1.4.5) lib/rack/chunked.rb:43:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
独角兽 (4.6.2) lib/unicorn/http_server.rb:552:in `process_client'
独角兽 (4.6.2) lib/unicorn/http_server.rb:632:in `worker_loop'
独角兽 (4.6.2) lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
独角兽 (4.6.2) lib/unicorn/http_server.rb:142:in `start'
独角兽 (4.6.2) bin/unicorn:126:in `'
/usr/local/rbenv/versions/1.9.3-p194/bin/unicorn:23:in `load'
/usr/local/rbenv/versions/1.9.3-p194/bin/unicorn:23:in `'

我不知道具体是什么原因,但我的看法是:

方法中的 UsersController_default_wrap_model尝试访问 Profile 模型(没有命名空间)并引发错误。因为当我重命名配置文件模型时(例如:信息)我没有错误,一切都很好。

所以我的问题是:如何避免这个错误?

4

1 回答 1

1

问题是因为我将带有命名空间模型的目录添加到 autoload_paths:

#/config/application.rb
config.autoload_paths += %W(#{Rails.root}/app/models/user)

当我删除它时,一切都很好。

于 2013-04-01T00:46:57.237 回答