ruby - 单一嵌套资源的正确路线

标签 ruby ruby-on-rails-3 url routes nested-resources

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

parent_resourcechild_resource

这给了我以下路线:

somesite.com/parent_resources/14
somesite.com/parent_resources/14/child_resources/1

然而,对于每个 parent_resource,永远只有一个 child_resource,因此对于使用该网站的人来说,这非常令人困惑。我希望 child_resource 路径看起来像这样:

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 

从 Rails 指南到路由:

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 生成:

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

按照惯例,子 Controller 仍将是复数形式的 ChildResourcesController。

Rails 有一个很好的路由指南。请参阅有关 singular resources 的部分.

关于ruby - 单一嵌套资源的正确路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11297015/

相关文章:

ruby-on-rails - 找不到 gem rails - Gem::GemNotFoundException

OSX 上的 python 和 echo -n

javascript - Rails 3 - 未找到一些必需的 JS

apache - 将特定 URL 重定向到带有 SEO 名称的 SSL 版本

ruby - 我可以确定 RubyGems 将哪些路径添加到加载路径以使我的命令行应用程序正常工作吗?

ruby - RSpec should be_true vs should == true

ruby-on-rails-3 - 无法在生产模式下访问 config.handlebars

ruby-on-rails - Rails 3 路由错误 : No route matches {:controller= >"user_sessions"}

ios - 将属性转换为 URL/NSURL

url - 如何使用 mod_rewrite 将查询字符串转换为路径 URL?