将分页、嵌套路线、ruby、rails
我试图让 will 分页链接到我的嵌套路由而不是常规的 posts 变量。我知道我应该传递一些参数来分页,但我不知道如何传递它们。
基本上,@posts
中存储了一个数组,而 paginate 可以访问的另一个参数是 category_id
。
嵌套路由是 /category/1/posts
但点击下一个和上一个将分页返回像这样 posts?page=1&category_id=7
的 URL。
<%= will_paginate @most_recent_posts "What do I do here?" %>
这是 Yannis 回答的结果:
在您的控制器中,您可以执行以下操作:
@posts = @category.posts.paginate
在您看来:
<%= will_paginate(@post) %>
执行此操作会产生以下 URL
posts?page=2&post_category_id=athlete_management
paths.rb #there are moreroutes but those are therelevant those
map.resources :posts
map.resources :post_categories, :has_many => :posts
解决方案
map.resources :post_categories do |post_category|
post_category.resources :posts
end
map.resources :posts
必须在之后声明资源块
谢谢斯蒂芬!
I'm trying to get will paginate to link to my nested route instead of the regular posts variable. I know I'm supposed to pass some params to paginate but I don't know how to pass them.
Basically there is an array stored in @posts
and the other param paginate has access to is category_id
.
The nested route is /category/1/posts
but hitting next and previous on will paginate returns a url like this posts?page=1&category_id=7
.
<%= will_paginate @most_recent_posts "What do I do here?" %>
This is the result of Yannis's answer:
In your controller you can do:
@posts = @category.posts.paginate
And in your view:
<%= will_paginate(@post) %>
Doing this comes up with the following URL
posts?page=2&post_category_id=athlete_management
routes.rb #there are more routes but these are the relevant ones
map.resources :posts
map.resources :post_categories, :has_many => :posts
solution
map.resources :post_categories do |post_category|
post_category.resources :posts
end
map.resources :posts
Had to declare the resource after the block
Thanks stephen!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为我遇到了同样的问题,所以在这里跳伞解决这个老问题。
我遇到了几乎完全相同的问题。我听从了响应者的建议,认为路线上发生了一些奇怪的事情。我深入研究了路线并发现(根据您的情况进行翻译):
这使得调用
category_posts_path
返回(如预期)/category/1/posts
。然而,至关重要的是要认识到
will_paginate
调用类似于url_for
的东西,它从路由“向后”工作以找到与参数匹配的第一个路由。由于
resources :posts
出现在嵌套路由上方,因此它认为该路由满足要求,只需插入category_id=1
即可作为查询字符串。它对其他人来说“开箱即用”的原因是因为他们没有将嵌套资源单独列为独立资源。
删除它,你应该没问题!
Parachuting in here on this old question because I encountered the same issue.
I had pretty much exactly the same problem. I followed the responders' advice that something funky was going on with the routes. I dug into the routes and found (translated to suit your situation):
This made it so that calling
category_posts_path
returned (as expected)/category/1/posts
.However, it's of pivotal importance to realize that
will_paginate
calls something that resemblesurl_for
, which works "backwards" from the routes to find the first route that matches the parameters.Since
resources :posts
appears above the nested route, it sees that that one satisfies the requirements and just insertscategory_id=1
was a query string.The reason it worked "out of the box" for everyone else was because they didn't have the nested resource separately listed as a standalone resource.
Remove that and you should be fine!
在您的控制器中,您可以执行以下操作:
在您看来:
In your controller you can do:
And in your view:
好的,所以问题并不清楚,但我假设您正在尝试从根级 URL(如
/posts
)进入特定类别的页面(如/category/) 1/帖子
。我认为这是因为我有嵌套路由(甚至在命名空间中),其中 will_paginate 可以正常工作,无需任何特殊参数。如果情况并非如此,那么您的路线可能发生了其他奇怪的事情,并且需要更多信息来调试。
不过我至少可以解释 will_paginate 是如何工作的。当它生成页面 URL 时,它会使用现有参数以及所需的特定页面调用
url_for
。因此,如果您已经设置了一堆参数,则应该保留它们,包括属于路径本身一部分的参数。幸运的是,它还允许您传递额外的参数以与(惊喜!):params
参数合并到 will_paginate。现在假设您已正确设置嵌套路由,则可以通过以下方式生成
/category/1/posts
:但是如果您位于页面
/posts
上,则参数将仅be:所以你需要将category_id传递给will_paginate,如下所示:
如果这不起作用,那么我怀疑你的路线有问题。尝试在模板中放入调试语句,并使用不同的参数排列调用
url_for
。请记住,路由定义顺序很重要,并且可能会产生冲突。Okay, so it's not clear from the question exactly, but I'm assuming that you are trying to go from a root-level URL like
/posts
into pages for specific categories like/category/1/posts
.The reason I assume this is because I have nested routes (in a namespace even) where will_paginate works fine without any special params. If that is not the case then there is probably something else funky going on with your routes and more information will be needed to debug.
However I can at least explain how will_paginate works. When it's generating the page URLs it calls
url_for
with existing params plus the specific page needed. Therefore, if you already have a bunch of params set, it should preserve them, including ones that are part of the path itself. Fortunately it also allows you to pass additional params to merge in with the (surprise!):params
parameter to will_paginate.Now assuming you have the nested routes set up properly,
/category/1/posts
can be generated by:But if you are on the page
/posts
then the params will only be:So you need to pass the category_id to will_paginate like so:
If this doesn't work, then I suspect something wrong with your routes. Try putting a debug statement in the template and calling
url_for
with different permutations of the params. Remember that the route definition order matters and could be creating conflicts.