如何避免rails中2级嵌套路由中的grand_parent id?

时间:2016-03-30 07:48:19

标签: ruby-on-rails-4.2 ruby-2.1.3

我的路线档案:

resources :users do 
  resources :posts, shallow: true do
    resources :comments, shallow: true
  end
end  

现在,索引的评论路线如下所示: -

"/users/:user_id/posts/:post_id/comments"  

现在,我想只获得以下类型的网址: -

"/posts/:post_id/comments"    

即。我想跳过grand_parent部分。我怎样才能做到这一点,谢谢。

1 个答案:

答案 0 :(得分:0)

在grand_parent中简单地输入shallow:true选项给出了解决方案:

resources :users, shallow: true do
  resources :posts do
    resources :comments  
  end
end