不使用资源的注释嵌套路由



我正在建立一个论坛,线程设置和查看没有问题。在尝试创建一个线程内的评论的过程中,我似乎对如何设置路由感到困惑。通常我知道人们有嵌套的路由:

resources :threads do
  resources :comments
end

尽管不同之处在于我的路由不使用资源方法,因为我已经单独设置了它们:

get '/thread' => 'threads#discussion'
post '/create_thread' => 'threads#create'
get '/create_thread' => 'threads#new', :as => :new_thread
get '/threads/:id' => 'threads#show', :as => :thread_show
get 'threads/edit/:id' => 'threads#edit', :as => :edit_thread
put '/threads/edit/:id' => 'threads#update', :as => :update_thread
delete 'threads/:id' => 'threads#destroy'

线程/线程替换实际位置名称

是否有一种方法可以让我在每个线程内放置评论的嵌套路由?我已经为每个模型(用户,线程,评论)提供了适当的关联,并将user_id, thread_id添加到评论模型中。

如果你有任何关于正确设置评论路由的知识,或者知道一篇文章,请在这里列出。

非常感谢!

编辑

注释将直接放在线程页上,而不是一个新的页面。

你可以像这样"手动"嵌套路由:

get '/threads/:thread_id/comments/new' => 'comments#new'
get '/threads/:thread_id/comments/:id' => 'comments#show'

和CommentsController的new动作可以做这样的事情:

def new
  @thread = Thread.find(params[:thread_id])
  @comment = @thread.comments.build
end

然而,我强烈同意乔丹上面的建议;你的路由有点乱,不是完全RESTful的,通过资源路由更容易表达。

最新更新