我在Company
class Company::Employee
我有一个控制器,用于我的员工的个人资料,在Employee
class Employee::ProfilesController < EmployeeController
我在该控制器中设置@employee
@employee = Company::Employee.find(...)
但我有错误
未定义的方法`find'for Employee:Module
似乎Company::Employee
已解析为托管Employee
的{{1}}模块。
当我在那里看到时,我有一条额外的消息Employee::ProfilesController
。如何通过模型类引用控制器模块?
我尝试了一些内容,包括warning: toplevel constant Employee referenced by Company::Employee
和::Company::Employee
,但没有任何变化。
相关文件位于
require 'company/employee'
app/controllers/employee/profiles_controller.rb
微量
app/models/company/employee.rb
类
NoMethodError (undefined method `find' for Employee:Module):
app/controllers/employee/profiles_controller.rb:30:in `set_company_employee'
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (7.0ms)
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.5ms)
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.
0ms)
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescue
s/layout (2726.3ms)
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/web-console-3.1.1/lib/web_console/templates/_markup.html.erb (0.5ms)
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/web-console-3.1.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (
1.0ms)
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/web-console-3.1.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.5
ms)
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/web-console-3.1.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.5ms)
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/web-console-3.1.1/lib/web_console/templates/console.js.erb within layouts/javascript (2507.8ms)
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/web-console-3.1.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.5ms)
Rendered C:/dev/Ruby22-x64/lib/ruby/gems/2.2.0/gems/web-console-3.1.1/lib/web_console/templates/index.html.erb (4939.1ms)
配置
答案 0 :(得分:0)
我发现this nice article解释了自动加载可能是一团糟。
我仍然不确定为什么在我的特定情况下会发生此错误,但有些评论指出require_dependency
可以帮助我,并且确实如此:
在我的Company::Employee
控制器上添加这两行之后,一切似乎都运行良好
(请注意,我还需要添加第二个,或者我得到NoMethodError (undefined method 'where' for Company:Class):
# app/controllers/company/profiles_controller.rb
require_dependency "company/employee.rb"
require_dependency "company.rb"