嵌套路由中的附加参数

时间:2012-09-30 21:43:39

标签: ruby-on-rails ruby routes

我有嵌套资源:

resources :posts do
  resources :comments
end

我当前的link_to是这样的:

<%= link_to 'Reply', [:new, @post, :comment] %>

导致:

http://example.com/posts/8/comments/new

但我需要这样的链接:

http://example.com/posts/8/comments/new?parent_id=7

如何使用link_to创建该链接?

2 个答案:

答案 0 :(得分:1)

我能用一个匹配声明得到它:

match "(/bazess/:baz_id)(/foos/:foo_id)/bars(/page/:page)(/:format)" => "bars#index", :as => :bars

您也可以参考this article on nested resources from The Rails 3 Way

答案 1 :(得分:1)

在我发表评论后发表回答:

new_post_comment_path(@post, :parent_id => 7)