0

我的路线文件中有以下内容:

namespace :forum do
  resources :topics
  resources :posts do
    resources :comments
  end
end

所以主题和帖子控制器在论坛模块(Forum::TopicsControllerForum::PostsController)中,但评论控制器不是(它只是CommentsController),因为它是多态的(在一些控制器之间共享)。

问题是应用程序寻找的Forum::CommentsController显然不存在,我怎样才能为该资源定义没有模块?

我试过了,但没有奏效:

namespace :forum do
  resources :topics
  resources :posts do
    resources :comments, controller => 'comments'
  end
end

任何帮助将不胜感激,谢谢!

4

3 回答 3

1

Ok, I guess you want to re-use the codes in Comments controller. Is that right? If so, I do not know any direct answer to this question but I guess you may consider something like that.

comments_controller.rb

module forum
  module posts
   class CommentsController < ApplicationController
     include Commentable
   end
  end
end

commentable.rb

module Commentable 

end

Now you can use this commentable module anywhere you want to use. So, you are re-using common codes with two features: 1. you get freedom to change behavior per controller 2. you have to create separate files for each controller.

于 2013-03-11T04:41:51.940 回答
1

它不是最干净的(我不确定是否有更好的方法来实现这一点),但您可以使用范围而不是命名空间,然后显式设置论坛命名空间中资源的控制器:

scope '/forum' do
  resources :topics, controller => 'forum/topics'
  resources :posts, controller => 'forum/posts' do
    resources :comments
  end 
end
于 2013-03-11T06:15:42.460 回答
1

您也可以手动设置评论的路线

例如

match '/forums/:forum_id/posts/:post_id/comments/:comment_id' => 'comments#show'

所以你的评论控制器不必坐在其他模块内

于 2013-03-11T05:50:57.860 回答