简体   繁体   English

will_paginate页面链接问题

[英]Issue with will_paginate page links

I currently have a comment model that posts under a micropost and both are displayed on the same page. 我目前有一个评论模型,在微博下发布,两者都显示在同一页面上。 The issue is that both are displayed on the same page and both are paginated and I am trying to go for the facebook approach to microposting. 问题是两者都显示在同一页面上,并且两者都是分页的,我试图用facebook方法进行微观测量。 Here is the issue below: 以下是以下问题:

The links for both pagination turns into this href="/users/2?page=2" rather than href="/users/2/micropost?page=2" or href="/users/2/comment?page=2" . 两个分页的链接变成这个href="/users/2?page=2"而不是href="/users/2/micropost?page=2"或者href="/users/2/comment?page=2" I am unsure how to go about solving this problem. 我不确定如何解决这个问题。 Here are some of my code. 这是我的一些代码。 All suggestions are much appreciated! 所有建议都非常感谢!

Micropost Render HTML Micropost渲染HTML

<table class="microposts">
<% if microposts.any? %>
<%= render microposts %>
<%= will_paginate microposts, :page_links => false %>
<% else %>
<div class="EmptyContainer"><span class='Empty'>Add a thread!</span></div>
<% end %>
</table>

Comment Section HTML 评论部分HTML

<div id='CommentContainer-<%= micropost.id%>' class='CommentContainer Condensed2'>
<div class='Comment'>
<%= render :partial => "comments/form", :locals => { :micropost => micropost } %>
</div>
<div id='comments'>
  <% comments = micropost.comments.paginate(:per_page => 5, :page => params[:page]) %>
  <%= render comments %>
  <%= will_paginate comments, :class =>"pagination" %>
</div>
</div>

User Controller for the Show Page 显示页面的用户控制器

  def show
    @user = User.find(params[:id])
    @comment = Comment.find(params[:id])
    @micropost = Micropost.new
    @comment = Comment.new
    @comment = @micropost.comments.build(params[:comment])
    @comments = @micropost.comments.paginate(:page => params[:page], :per_page => 5)
    @microposts = @user.microposts.order('created_at DESC').paginate(:per_page => 10, :page => params[:page])
      respond_to do |format|
      format.html
      format.js
     end
  end

Problem lies within will_paginate way of creating urls for each page (it doesn't have anything to do with jQuery). 问题在于为每个页面创建URL的will_paginate方式(它与jQuery没有任何关系)。

By design, will_paginate try its best to guess what's the base url for the page user is on (internally it's using controller/action to do that). 通过设计,will_paginate会尽力猜测页面用户的基本URL是什么(在内部它使用控制器/动作来做到这一点)。 That base url is then combined with any extra params passed to will_paginate helper using :params and succesive page numbers. 然后将该基本URL与传递给will_paginate助手的任何额外参数组合使用:params和succesive页码。

For now (will_paginate 3.0.3), in order to overwrite this default behavior, you need to write your custom LinkRenderer class. 目前(will_paginate 3.0.3),为了覆盖此默认行为,您需要编写自定义LinkRenderer类。 Below there's example of such class - it makes use of new, extra option :base_link_url that can be passed to will_paginate view helper. 下面是这样一个类的例子 - 它使用了新的额外选项:base_link_url ,可以传递给will_paginate视图助手。 Passed string is then used as a base when creating pagination links. 然后,在创建分页链接时,将传递的字符串用作基础。 If :base_link_url option is not passed, it will fallback to default behavior. 如果:base_link_url未传递:base_link_url选项,则它将回:base_link_url默认行为。

Put following class somewhere rails can find it on load (/lib for example, provided you've added /lib to your autoload paths in application.rb ): 将以下类放在某个地方,rails可以在加载时找到它(例如/ lib,只要你将/ lib添加到application.rb的自动加载路径):

# custom_link_renderer.rb
class CustomLinkRenderer < WillPaginate::ActionView::LinkRenderer
  def prepare(collection, options, template)
    @base_link_url = options.delete :base_link_url
    @base_link_url_has_qs = @base_link_url.index('?') != nil if @base_link_url
    super
  end

  protected
  def url(page)
    if @base_link_url.blank?
      super
    else
      @base_url_params ||= begin
        merge_optional_params(default_url_params)
      end

      url_params = @base_url_params.dup
      add_current_page_param(url_params, page)

      query_s = []
      url_params.each_pair {|key,val| query_s.push("#{key}=#{val}")}

      if query_s.size > 0
        @base_link_url+(@base_link_url_has_qs ? '&' : '?')+query_s.join('&')
      else
        @base_link_url
      end
    end
  end
end

Usage: 用法:

# in your view
will_paginate collection, :renderer => CustomLinkRenderer, :base_link_url => '/anything/you/want'

And now back to your case. 现在回到你的案子。 By this time you probably see the solution - you can have two will_paginate widgets on one page with different base urls by passing different :base_link_url options for those two. 到这个时候你可能会看到解决方案 - 你可以在一个页面上有两个带有不同基本URL的will_paginate小部件,为这两个提供不同的:base_link_url选项。

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

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