具有多个参数的两条路径到单一控制器的方法?

时间:2014-05-21 12:14:23

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 routes url-routing

我们假设我有模型:ArticlePostComment。对于这些模型,我需要在CommentsController#showarticles中映射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或其他东西在两个资源中创建具有多个参数的单个控制器方法的路由?

0 个答案:

没有答案