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

发布于 2024-11-17 09:46:30 字数 1014 浏览 9 评论 0原文

我陷入了一个涉及 Rails 3 路线的非常棘手的问题,而且我自己的路线不够好,无法解决这个问题。我有一个“评论”资源,只有“显示”和“索引”操作。我正在使用Friendly ID gem 来处理被攻击的ID,并且我还有一个嵌套评论资源,它允许人们对评论发表评论。

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

因此,到目前为止,我的索引页路径只是“评论”,以及用于显示操作的“评论/矩阵”。到目前为止很简单。

首先,我在主索引页上遇到了一些障碍。我在索引页面上有 will_paginate gem,并且正在缓存该页面。问题是,当路径类似于 /reviews?page=2 时,缓存将不起作用,而我在分页时需要路径为 /reviews/2/ ,所以我在上面添加了之前的评论资源使这项工作发挥作用。

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

为什么有约束?因为我的“显示”资源路线不起作用,因为上面的行(没有数量限制)最终成为干扰“显示”路线的“包罗万象”。由于显示操作的 ID 始终是单词,而索引上的分页页面始终有一个数字,这就是我这样做的原因。

但事情是这样的,我不希望“展示”路线只是简单的“评论/矩阵”。看,所有评论都是按所评论的事物类型进行分类的,所以我真的希望“显示”路径类似于“评论/电影/矩阵”或“评论/书籍/指环王” .'

我宁愿使用原始评论“显示”路径来显示其他类别索引页面,例如“评论/书籍”。 (上述约束的另一个原因。)但我将把它保存到另一个SO问题,因为我不想让这个问题变得太复杂。

那么回到上一个问题。通常情况下,我认为上述问题可以通过简单的“匹配”路由(使用路由通配?)来解决,除非我们在评论资源中拥有这些嵌套注释。如何制作如上所述的“显示”页面,同时保留评论上的评论嵌套资源?

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. 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.

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. 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. 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.

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.

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 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. How do I go about making the 'Show' pages like the above, while retaining the Comments nested resource on Reviews?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2024-11-24 09:46:30

没有汗水(如果这就是你的意思)。

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 => /.+/}
  • 将约束添加到第三条指令将允许您在 :path 中使用 /。 (这是真的:完全不使用任何约束将不允许 foo/bar 匹配此模式。)(除了正则表达式 /.+/,您可能希望 /[a-zA-Z/]+/,但这并不重要,因为早期的路径已经捕获了不应该是 #show 的所有内容。)
  • 顺序很重要。保持这三者出现的顺序。

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. (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.)
  • Order matters. Keep these three in the order that they appear.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文