正确的单一嵌套资源的路由

Und*_*ion 3 ruby url routes nested-resources ruby-on-rails-3

我有2个资源,一个是另一个的嵌套资源:

parent_resourcechild_resource.

这给了我以下路线:

somesite.com/parent_resources/14
somesite.com/parent_resources/14/child_resources/1
Run Code Online (Sandbox Code Playgroud)

然而child_resource,每个parent_resource人只有一个单独,所以对于使用该网站的人来说,这是非常令人困惑的.我希望child_resource路径看起来像这样:

somesite.com/parent_resource/14/child_resource
somesite.com/parent_resource/14/child_resource/edit
etc
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?

我的routes.rb

resources :parent_resources do

   resource :child_resource do
   end

end 
Run Code Online (Sandbox Code Playgroud)

从导轨到导航:

A singular resourceful route generates these helpers:

new_geocoder_path returns /geocoder/new
edit_geocoder_path returns /geocoder/edit
geocoder_path returns /geocoder
Run Code Online (Sandbox Code Playgroud)

那节目怎么样?

我的路线由佣金路线产生:

parent_resource_child_resource      POST   /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#create


new_parent_resource_child_resource  GET    /parent_resources/:parent_resource_id/child_resource/new(.:format)             child_resources#new

edit_parent_resource_child_resource GET    /parent_resources/:parent_resource_id/child_resource/edit(.:format)            child_resources#edit

                                    GET    /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#show

                                    PUT    /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#update

                                    DELETE /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#destroy
Run Code Online (Sandbox Code Playgroud)

Rob*_*vis 6

在您的路由中,使用单数resource方法定义子资源:

resources :parent_resources do
  resource :child_resource
end
Run Code Online (Sandbox Code Playgroud)

按照惯例,孩子的控制器仍然是ChildResourcesController,复数.

Rails有很好的路由指南.请参阅有关奇异资源的部分.