在控制器中找不到命名空间内的 Ruby on Rails 模型 [英] Ruby on Rails model inside namespace can't be found in controller
问题描述
我是 Rails 新手,无法弄清楚这个问题...
I'm new to rails and can't figure out this issue...
我有一个控制器
Admin::Blog::EntriesController
在 app/controllers/admin/blog/entries_controller.rb 中定义
defined in app/controllers/admin/blog/entries_controller.rb
我有一个名为
Blog::Entry
在 app/model/blog/entry.rb 中定义
defined in app/model/blog/entry.rb
当我尝试从控制器访问我的模型时,我从这一行得到一个 未初始化的常量 Admin::Blog::EntriesController::Blog"
:
When I try to access my model from the controller, I get a "uninitialized constant Admin::Blog::EntriesController::Blog"
from this line:
@blog_entries = Blog::Entry.find(:all)
很明显,它没有正确找到命名空间,这很奇怪,因为根据我所阅读的内容,我已使用正确的语法将模型放在正确的文件夹中.
Clearly it is not finding the namespace correctly which is odd because according to what I have read, I have placed my model in the correct folder with the correct syntax.
关于如何解决这个问题有什么想法吗?
Any ideas on how I can fix this?
谢谢
推荐答案
尝试:
@blog_entries = ::Blog::Entry.find(:all)
它当前正在寻找错误的类.在 Blog
之前使用 ::
将强制它从顶层查看.
It's currently looking for the wrong class. Using ::
before Blog
will force it to look from the top level.
这篇关于在控制器中找不到命名空间内的 Ruby on Rails 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!