我们假设我有模型:Article
,Post
和Comment
。对于这些模型,我需要在CommentsController#show
和articles
中映射posts
。像这样:
resources :articles do
resources :comments, :only => [:show]
end
resources :posts do
resources :comments, :only => [:show]
end
这完美无缺,即它会产生类似这样的路线:
/articles/:article_id/comments/:id
/posts/:post_id/comments/:id
两者都指向同一个控制器的单一方法:CommentsController#show
我需要为帖子添加更多/多个参数,同时保留与CommentsController
相同的网址路径:
/articles/:article_id/comments/:id
/posts/:post_id/:post_title/:post_year/comments/:id
我尝试match
,但第一次出现在resources :articles do...
会覆盖它。
resources :articles do
resources :comments, :only => [:show]
end
match '/posts/:post_id/:post_title/:post_year/comments/:id', :to => 'comments#show', :as => :show_post_comments
它会抛出错误:
没有路线匹配{:controller =>"评论",:action =>" show", :post_id => 10,:post_title =>"某些标题",:post_year =>" 1995",:id =>" 1"}
那么,是否可以使用get
或其他东西在两个资源中创建具有多个参数的单个控制器方法的路由?