Ruby on rails 具有多个参数的两条路由到单个控制器';什么方法?
假设我有模型:Ruby on rails 具有多个参数的两条路由到单个控制器';什么方法?,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.2,routes,url-routing,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.2,Routes,Url Routing,假设我有模型:文章,帖子,和评论。对于这些模型,我需要在文章和帖子中映射CommentsController#show。大概是这样的: resources :articles do resources :comments, :only => [:show] end resources :posts do resources :comments, :only => [:show] end /articles/:article_id/comments/:id /posts/:
文章
,帖子
,和评论
。对于这些模型,我需要在文章和帖子中映射CommentsController#show
。大概是这样的:
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
这非常有效,也就是说,它将生成如下路径:
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
的相同URL路由:
/articles/:article_id/comments/:id
/posts/:post_id/:post_title/:post_year/comments/:id
我尝试了match
,但第一次出现在resources:articles中…
覆盖了它
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=>“comments”,:action=>“show”,
:post_id=>10,:post_title=>some title,:post_year=>1995,:id=>1}
那么,是否有可能使用get
或其他方法在两个资源中创建一个包含多个参数的路由到单个控制器的方法 为什么不发送这些参数将它们附加到URL?因为有一个JavaScript,每当用户选择一条注释并单击提交时,它会将注释id附加到提交按钮路径的末尾。因此,如果没有办法解决这个问题,那么我必须重写JavaScript,用斜杠“/”拆分,并在?post_title='some title'
等之前附加id。您可以向URL附加多个参数,例如,/posts?abc=1&def=2&name=rsb
bundle exec-rake路由为该路由显示什么(用匹配定义的代码)另外,最好使用get
而不是match
.P.S.是的,这是可能的。@RSB-它不是用于帖子,而是用于评论,我有这样的东西:/posts/11/comments/?post\u title=sss&post\u year=1995
我必须修改JavaScript以从元素拆分并加入此数据url以生成此:/posts/11/comments/1?post_title=sss和post_year=1995
基于所选评论的id。我希望现在清楚了?