如何在嵌套资源中创建浅层路由?

时间:2013-06-07 18:40:38

标签: ruby-on-rails ruby routes

如何设置路线文件以创建以下路线:

/forums/1/posts           # GET  index
/forums/1/posts           # POST create
/forums/1/posts/new
# ... the other forum posts restful actions

/posts/1/votes            # POST create
/posts/1/votes/1/destroy

我试过这种方式:

resources :forums do
  resources :posts
end

resources :posts do
  resources :votes
end

这会创建多余的/posts/1/posts/new网址,而不是forum

我也尝试过:

resources :forums do
  resources :posts do
    resources :votes, only: [:create, :destroy], shallow: true
  end
end

但这会创建forums/1/posts/1/votes/create,而我只想要/posts/1/votes/create

基本上,我不想将votes资源嵌套在forums资源下。

resources :posts do # would be nice to do resources :posts, none: true, do
  resources :votes
end

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用:none => true代替:only => []

resources :forums do
  resources :posts
end

resources :posts, :only => [] do
  resources :votes, :only => [:create, :destroy]
end