假设您有两个模型,其博客文章和评论设置如下:
class post
has_many :comments
路由的设置方式几乎相同:
map.resources :posts, :has_many => :comments
当我发表新评论时,它显示为 localhost::3000/postname/comments/new
为了使 url 看起来像:localhost::3000/postname/shoutout,你应该做什么?
我想这样做的原因是因为这个特定页面上将不仅仅有一个新的评论表单。
我在命名路由时没有遇到任何问题,但我在弄清楚如何处理嵌套路由时遇到了困难。
最佳答案
map.resources :posts, :has_many => :comments, :collection => {:shoutout => :get}
关键功能是:collection
,它指向对:'name' => 'method',并且您需要在 Controller (和 View )中实现此名称
关于ruby-on-rails - Rails 中嵌套命名路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2113348/