Rails 3 从不同的视图显示 will_paginate 页面

发布于 2024-11-01 08:03:39 字数 583 浏览 4 评论 0原文

我正在使用 Rails 3 和 will_paginate。我已经 will_paginate 成功地在三重嵌套资源中工作。我无法从其他控制器链接到分页视图(第 1 2 3 4 页)。我在一个控制器中,但我的分页发生在嵌套控制器中。假设我有一个控制器“部分”“主题”和“回复”。在我的节目“主题”控制器中,我对所有回复进行分页(因为它根据主题的路径提取回复)。所以现在我在显示部分视图中尝试链接到该主题的不同页面(每页大约 7 个回复)。由于我位于部分控制器中,因此我无法轻松地对主题的回复进行分页,因为它依赖于视图中的主题循环。此外,我不一定想要主题名称旁边的“上一个”和“下一个”链接...我只需要 (1 2 3 ... 10) 或类似的内容。有没有办法保存此分页以便在不同的控制器中使用?

做到这一点最简单的方法是什么?

如果您很难理解,该视图就像链接到不同页面的典型论坛帖子:

Section
    Topic A (1 2 3)
    Topic B (1)
    Topic C (1 2 3 ...5)
    Topic D (1 2 3 ...10)

但我不确定如何使用对分页一无所知的控制器中的 will_paginate 。

I'm using Rails 3 and will_paginate. I have will_paginate successfully working in a triple nested resource. I'm having trouble linking to the paginate view (page 1 2 3 4) from a different controller. I'm in one controller, but my pagination happens in the nested controller. Say I have a controllers 'section' 'topics' and 'replies'. In my show 'topics' controller I do the pagination for all of the replies (because it pulls replies based on the path of the topic). So now I'm in the show section view trying to link to the different pages of the topic (about 7 replies per page). Since I'm in the section controller, I can't easily paginate the replies of a topic, because it is dependent on a loop of topics in the view. Furthermore, I don't necessarily want the 'previous' and 'next' links next to the topic name... I just need (1 2 3 ... 10) or something similar. Is there a way to save this pagination for use in a different controller?

What is the easiest way to do this?

In case you're having a hard time understanding, the view is like a typical forum post that links to different pages:

Section
    Topic A (1 2 3)
    Topic B (1)
    Topic C (1 2 3 ...5)
    Topic D (1 2 3 ...10)

But I'm not sure how to use the will_paginate from a controller that knows nothing about the pagination.

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

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

发布评论

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

评论(1

云淡风轻 2024-11-08 08:03:39

抱歉回答我自己的问题。我花了一段时间才弄清楚这一点。

我在没有使用 will_paginate 显示方法的情况下弄清楚了。我做了我自己的帮手。我不知道这是否是“正确”的做事方式,但它确实有效。
我制作了一个助手来输出主题旁边的页码。在我看来,我有一个这样的小组:

<% @topics.each do |topic| %>
    <%= pageLinks(topic, @section) %>
<% end %>

那么在助手中,我做了这样的事情:

def pageLinks(t, s)
   html = ""
   if t.replies.count > 7
      pageList = []
      1.upto(t.replies.count/6) {|n| pageList << "#{link_to n, section_topic_path(s, t, :page =>n) } "}      
      html << "| page (#{pageList})"
      html.html_safe
   else
      return
   end
end

基本上我除以我想要的每页结果数量,并据此进行链接。

这段代码的一个有趣之处是 link_to :page => 花了我一段时间。 n 显示路径 /section/id/topic/id?page=n... 如果您使用“rake 路由”中列出的路径,则必须将其传递到路径的参数中。我也很难返回实际的 html,而不是打印 html 本身。

Sorry for answering my own question. It took awhile to figure this out.

I figured it out without using the will_paginate display method. I made my own helper. I don't know if this is the 'correct' way to do things, but it works.
I made a helper to output page numbers next to the topics. In my view, I had a group like:

<% @topics.each do |topic| %>
    <%= pageLinks(topic, @section) %>
<% end %>

So then in the helper, I made it like this:

def pageLinks(t, s)
   html = ""
   if t.replies.count > 7
      pageList = []
      1.upto(t.replies.count/6) {|n| pageList << "#{link_to n, section_topic_path(s, t, :page =>n) } "}      
      html << "| page (#{pageList})"
      html.html_safe
   else
      return
   end
end

Basically I divide by the amount of results per page I want and link based on that.

An interesting thing about this code that took me awhile was the link_to :page => n that displays the path /section/id/topic/id?page=n... You have to pass it in a parameter of the path if you're using the path listed in 'rake routes'. I also had a hard time returning the actual html rather than than printing the html itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文