命名空间控制器导致 Rails-3 中出现错误
我正在使用 ActsAsTaggableOn 向我的应用程序添加标记。除了我从这个 gem 获得的功能之外,我还想添加一个 TagsController 并基本上将标签视为我的应用程序中的任何其他资源。
我创建了tags_controller.rb,其中包含
class ActsAsTaggableOn::TagsController < ApplicationController
# ...
end
并在我的routes.rb中添加了
resources :tags, :module => :acts_as_taggable_on
当我运行rake paths
时,我得到
tags GET /tags(.:format) {:action=>"index", :controller=>"acts_as_taggable_on/tags"} POST /tags(.:format) {:action=>"create", :controller=>"acts_as_taggable_on/tags"} new_tag GET /tags/new(.:format) {:action=>"new", :controller=>"acts_as_taggable_on/tags"} edit_tag GET /tags/:id/edit(.:format) {:action=>"edit", :controller=>"acts_as_taggable_on/tags"} tag GET /tags/:id(.:format) {:action=>"show", :controller=>"acts_as_taggable_on/tags"} PUT /tags/:id(.:format) {:action=>"update", :controller=>"acts_as_taggable_on/tags"} DELETE /tags/:id(.:format) {:action=>"destroy", :controller=>"acts_as_taggable_on/tags"}
......这对我来说都是合理的。
但是,当我点击 localhost:3000/tags 时,我收到此错误:
LoadError (Expected MyApp/tags_controller.rb to define TagsController)
如果我尝试在控制台中评估 ActsAsTaggableOn::TagsController ,我会收到基本相同的错误:
LoadError: Expected MyApp/app/controllers/tags_controller.rb to define TagsController from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:492:in `load_missing_constant' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:503:in `load_missing_constant' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing' from (irb):1 from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands/console.rb:44:in `start' from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands/console.rb:8:in `start' from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands.rb:23:in `' from script/rails:6:in `require' from script/rails:6:in `'
我做错了什么?
I am using ActsAsTaggableOn to add tagging to my application. In addition to the features i get from this gem, I would also like to add a TagsController and basically treat tags as any other resource in my app.
I have created tags_controller.rb which contains
class ActsAsTaggableOn::TagsController < ApplicationController
# ...
end
and in my routes.rb i have added
resources :tags, :module => :acts_as_taggable_on
When I run rake routes
i get
tags GET /tags(.:format) {:action=>"index", :controller=>"acts_as_taggable_on/tags"} POST /tags(.:format) {:action=>"create", :controller=>"acts_as_taggable_on/tags"} new_tag GET /tags/new(.:format) {:action=>"new", :controller=>"acts_as_taggable_on/tags"} edit_tag GET /tags/:id/edit(.:format) {:action=>"edit", :controller=>"acts_as_taggable_on/tags"} tag GET /tags/:id(.:format) {:action=>"show", :controller=>"acts_as_taggable_on/tags"} PUT /tags/:id(.:format) {:action=>"update", :controller=>"acts_as_taggable_on/tags"} DELETE /tags/:id(.:format) {:action=>"destroy", :controller=>"acts_as_taggable_on/tags"}
… which all looks reasonable to me.
However, when I hit localhost:3000/tags i get this error:
LoadError (Expected MyApp/tags_controller.rb to define TagsController)
If I try to evaluate ActsAsTaggableOn::TagsController
in the console I get basically the same error:
LoadError: Expected MyApp/app/controllers/tags_controller.rb to define TagsController from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:492:in `load_missing_constant' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:503:in `load_missing_constant' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each' from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing' from (irb):1 from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands/console.rb:44:in `start' from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands/console.rb:8:in `start' from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands.rb:23:in `' from script/rails:6:in `require' from script/rails:6:in `'
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rails 期望模块和类位于其命名空间的目录中。因此,您应该将控制器、视图和助手移动到
acts_as_taggable_on
目录中:app/controllers/acts_as_taggable_on/tags_controller.rb
app/views/acts_as_taggable_on/tags/index .html.erb
我不确定你为什么需要这个命名空间。如果您只想在现有模型上创建 REST 操作,则可以生成
scaffold_controller
。例如:Rails expects modules and classes to be in their namespace's directory. So you should move your controller, views and helpers in an
acts_as_taggable_on
directory:app/controllers/acts_as_taggable_on/tags_controller.rb
app/views/acts_as_taggable_on/tags/index.html.erb
I'm not sure why you want this namespace. If you only want to create REST actions on an existing model, you can generate a
scaffold_controller
. For example:Michael 关于命名空间控制器的目录结构是正确的。
话虽这么说,控制器不必与模型位于同一名称空间中。相反,您可以让名为 SomeController 的控制器与名为 SomeModel 的模型一起使用。您是否已尝试从控制器类中删除名称空间并看看出了什么问题?
Michaël is right about the directory structure for namespaced controllers.
That being said, a controller does not have to be in the same namespace as a model. On the contrary, you can have a controller named SomeController working with a model named SomeModel. Have you already tried to drop the namespace from the controller class and see what goes wrong?