在rails中嵌套命名路由?

时间:2010-01-21 22:11:06

标签: ruby-on-rails routes

假设您有两个模型,博客文章和评论设置如下:

class post 
has_many :comments

并且路由的设置方式几乎相同:

map.resources :posts, :has_many => :comments

当我发表新评论时,它显示为localhost :: 3000 / postname / comments / new

为了使网址读取如下所示,您应该怎么做:localhost :: 3000 / postname / shoutout?

我想这样做的原因是因为这个特定的页面不仅仅有一个新的评论表格。

我可以毫无困难地命名路线,但是我无法弄清楚如何处理嵌套路线。

2 个答案:

答案 0 :(得分:3)

map.resources :posts, :has_many => :comments, :collection => {:shoutout => :get}

主要功能是:collection,这些对的点数:'name'=> 'method',你需要在控制器(和视图)中实现这个名称

答案 1 :(得分:2)

路由与页面上的表单无关,我不确定问题是什么?

如果您想让/postname/shoutout转到CommentController #new,您需要手动映射路线:

map.connect '/:post_id/shoutout', :controller => 'comments', :action => 'new'