简体   繁体   English

Ruby on Rails-带有索引/显示页面和分页的审阅资源的定制路由

[英]Ruby on Rails - Custom routing for review resource with index / show page and pagination

I'm stuck on a very tricky issue involving Rails 3 routes, and not good enough with routes to figure this one out on my own. 我陷入了涉及Rails 3路线的一个非常棘手的问题,而对于依靠路线自己解决这个问题还不够好。 I have a "Reviews" resource with only a "Show" and "Index" action. 我只有一个“显示”和“索引”操作,其中包含一个“评论”资源。 I am using the Friendly ID gem for slugged IDs, and I also have a nested comments resource, which allows people to comment on reviews. 我正在使用“友好ID” gem来处理缓慢的ID,并且我还有一个嵌套的评论资源,该资源可让人们对评论发表评论。

resources :reviews, :only => [:index, :show] do
  resources :comments, :only => [:new, :create]
end 

So my paths thus far for the index page is simply 'reviews,' and something like 'reviews/the-matrix' for the show action. 因此,到目前为止,索引页面的路径只是“评论”,而对于show动作,则类似于“评论/矩阵”。 Simple enough so far. 到目前为止足够简单。

Now first off, I hit a snag of sorts with the main index page. 现在首先,我在主索引页面上遇到了一些麻烦。 I have the will_paginate gem on the index page, and I am caching the page. 我在索引页面上有will_paginate gem,并且正在缓存该页面。 Problem is, caching won't work when the path is something like /reviews?page=2, rather I need the path to be /reviews/2/ when going through pagination, so I added in the following above the previous review resource to make this work. 问题是,当路径类似于/ reviews?page = 2时,缓存将不起作用,而在进行分页时,我需要将路径设为/ reviews / 2 /,因此我将以下内容添加到了之前的回顾资源的上方:使这项工作。

match 'reviews(/:page)' => 'reviews#index', :constraints => { :page => /[0-9]/ }

Why the constraint? 为什么要约束? Because my 'Show' resource route doesn't work because the above line - without the number constraint - ends up being a "catch-all" that interferes with the 'Show' route. 因为我的“显示”资源路线无效,因为上面的行(没有数字约束)最终成为干扰“显示”路线的“包罗万象”。 Since the ID of the show action is always going to be words, while paginated pages on the index always has a number, this is why I did it this way. 由于show action的ID始终是单词,而索引上的分页页面始终带有数字,因此这就是我这样做的原因。

But here's the thing, I don't want the 'Show' route to be simply 'reviews/the-matrix.' 但这就是问题,我不希望“显示”路线只是“评论/矩阵”。 See, all reviews are categorized by type of thing being reviewed, so really I would like the 'Show' path to be something like 'reviews/movies/the-matrix,' or 'reviews/books/lord-of-the-rings.' 看,所有评论均按所评论事物的类型进行分类,因此,我真的希望“显示”路径类似于“评论/电影/矩阵”或“评论/书籍/指环王” 。”

I would rather use the original reviews 'Show' path for additional category index pages, like 'reviews/books.' 我希望将原始评论的“显示”路径用于其他类别索引页面,例如“评论/书”。 (Another reason for the constraint above.) But I am going to save that for another SO question, as I don't want to make this question too complicated. (上述约束的另一个原因。)但是我将其保存到另一个SO问题中,因为我不想使这个问题变得过于复杂。

So back to the previous question. 回到上一个问题。 Normally I imagine the above could be solved by a simple 'match' route (with route globbing?), except we have those nested comments in the Reviews resource. 正常情况下,我想可以通过简单的“匹配”路线(具有路线通配?)解决以上问题,但在Reviews资源中有那些嵌套的注释。 How do I go about making the 'Show' pages like the above, while retaining the Comments nested resource on Reviews? 在保留“评论”中的“嵌套”资源的同时,如何制作类似于上述的“显示”页面?

No sweat (if this is what you mean). 无汗(如果这是您的意思)。

match 'reviews(/:page)' => 'reviews#index', :constraints => {:page => /[0-9]+/}
match 'reviews/comments/:id' => 'comments#my_action', :constraints => {:id => /[0-9]+/}
match 'reviews/:path' => 'reviews#show', :constraints => {:path => /.+/}
  • Adding the constraint to the third instruction will allow you to use / in your :path . 将约束添加到第三条指令将使您可以在/ :path使用/ (It's true: using no constraints at all will not allow foo/bar to match this pattern.) (Alternative to the regex /.+/ , you might wish /[a-zA-Z/]+/ , but it shouldn't matter since the earlier paths already catch everything that shouldn't be a #show .) (的确是这样:完全不使用任何约束将不允许foo/bar匹配此模式。)(替代regex /.+/ ,您可能希望/[a-zA-Z/]+/ ,但不应这样做。无关紧要,因为较早的路径已经捕获了所有不应该#show 。)
  • Order matters. 顺序很重要。 Keep these three in the order that they appear. 保持这三个顺序出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM