Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 单个嵌套资源的正确路由_Ruby_Ruby On Rails 3_Url_Routes_Nested Resources - Fatal编程技术网

Ruby 单个嵌套资源的正确路由

Ruby 单个嵌套资源的正确路由,ruby,ruby-on-rails-3,url,routes,nested-resources,Ruby,Ruby On Rails 3,Url,Routes,Nested Resources,我有两个资源,一个是另一个的嵌套资源: 父资源和子资源 这为我提供了以下途径: somesite.com/parent_resources/14 somesite.com/parent_resources/14/child_resources/1 然而,对于每个父资源,只有一个子资源,因此对于使用该网站的人来说,这是非常令人困惑的。我希望子资源路径如下所示: somesite.com/parent_resource/14/child_resource somesite.com/parent_r

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

父资源
子资源

这为我提供了以下途径:

somesite.com/parent_resources/14
somesite.com/parent_resources/14/child_resources/1
然而,对于每个
父资源
,只有一个
子资源
,因此对于使用该网站的人来说,这是非常令人困惑的。我希望子资源路径如下所示:

somesite.com/parent_resource/14/child_resource
somesite.com/parent_resource/14/child_resource/edit
etc
正确的方法是什么

我的路线.rb

resources :parent_resources do

   resource :child_resource do
   end

end 
从导轨到布线:

A singular resourceful route generates these helpers:

new_geocoder_path returns /geocoder/new
edit_geocoder_path returns /geocoder/edit
geocoder_path returns /geocoder
但这场表演呢

rake routes生成的My routes:

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

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

resources :parent_resources do
  resource :child_resource
end
按照约定,子级的控制器仍然是ChildResourcesController,复数形式


Rails有很好的路由指南。请参阅上的部分。

谢谢。问题是,这不会为子资源show生成命名路由。我只有一个创建,一个编辑和一个新的。是的,你有。如果您的家长是“用户”,而您的孩子是“配置文件”,那么您应该拥有
user\u profile\u路径(@user)
I使用rake路由仅显示以下路径:家长资源\u孩子资源POST/parent\u资源/:家长资源\u id/child\u资源(:格式)子资源创建新的父资源子资源获取/父资源/:父资源id/子资源/新(:格式)子资源新编辑父资源子资源获取/父资源/:父资源id/子资源/编辑(:格式)子资源,请注意,singular resource方法应采用单数名称。输出中的复数“child_resources”似乎是错误的。很好。我不小心把它改成了复数。不幸的是,当我把它切换回来时,我只剩下相同的三条路线(只是单数)。parent\u resource\u child\u resource(@parent\u resource)从包含它的页面中产生一个未定义的方法错误。@KennyBania将其添加到问题中。您应该将复数名传递给
资源
,将单数名传递给
资源
。你把单数传给了两个人。啊,我看你已经更正了名字。请确认命名路由不起作用,而不是仅依赖于
rake routes
,后者有时可能会忽略起作用的命名路由。因为这个计划对我很有效。