问题will_paginate页面链接 [英] Issue with will_paginate page links

查看:144
本文介绍了问题will_paginate页面链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有这下micropost和两个职位都显示在同一页面上评论的模式。问题是,两者都显示在同一页面上无一不是分页,我试图去为Facebook的方式来microposting。这是下面的问题:

为分页的链接变成这个的href =/用户/ 2页=​​ 2,而不是的href =/用户/ 2 / micropost?页= 2 HREF =/用户/ 2 /发表评论?页= 2。我不知道该如何去解决这个问题。下面是我的一些code。所有的建议都多少AP preciated!

Micropost渲染HTML

 <表类=microposts>
<如果microposts.any%? %>
&其中;%=渲染microposts%GT;
&其中;%= will_paginate microposts,:page_links =>假%>
<%其他%GT;
< D​​IV CLASS =EmptyContainer><跨度类='空'>添加一个线程<!/ SPAN>< / DIV>
<%结束%GT;
< /表>
 

注释部分HTML

 < D​​IV ID ='CommentContainer-<%= micropost.id%>类='CommentContainer Condensed2'>
< D​​IV CLASS ='评论'>
<%=渲染:部分=> 意见/表,:当地人=> {:micropost => micropost}%GT;
< / DIV>
< D​​IV ID ='意见'>
  <%的意见= micropost.comments.paginate(:per_page => 5,:页=> PARAMS [:页面])%GT;
  &其中;%=呈现注释%GT;
  <%= will_paginate评论:类=>中分页%>
< / DIV>
< / DIV>
 

用户控制器的显示页面

 高清节目
    @user = User.find(PARAMS [:ID])
    @comment = Comment.find(PARAMS [:ID])
    @micropost = Micropost.new
    @comment = Comment.new
    @comment = @ micropost.comments.build(PARAMS [:评论])
    @comments = @ micropost.comments.paginate(:页=> PARAMS [:页面]:per_page => 5)
    @microposts = @ user.microposts.order('created_at DESC)分页(:per_page => 10:页=> PARAMS [:页面])。
      respond_to代码做|格式|
      的format.html
      format.js
     结束
  结束
 

解决方案

问题出在每个页面创建的URL(它没有什么做用jQuery)的will_paginate方法

在设计上,will_paginate尽力去猜测什么是基本URL的网页用户是在(内部它使用的控制器/动作做到这一点)。该基地的URL,然后与用传递给will_paginate帮助任何额外的PARAMS 组合:PARAMS 和succesive页码

目前(will_paginate 3.0.3),以覆盖此默认行为,您需要编写自定义LinkRenderer类。下面还有例如这样的类 - 它利用新的,额外的选项:base_link_url 可以传递到will_paginate视图助手。传递的字符串创建分页链接时,则用作基础。如果:base_link_url 不通过的选项,它会退回到默认行为

把下面的类的地方导轨可以找到它的负载(/ lib目录例如, application.rb中前提是你已经添加了/ lib添加到您的自动加载路径):

 #custom_link_renderer.rb
类CustomLinkRenderer< WillPaginate :: ::的ActionView LinkRenderer
  DEF prepare(收集,选择,模板)
    @base_link_url = options.delete:base_link_url
    @base_link_url_has_qs = @ base_link_url.index(?)!=零,如果@base_link_url
    超
  结束

  保护
  高清URL(页)
    如果@ base_link_url.blank?
      超
    其他
      @base_url_params || =开始
        merge_optional_params(default_url_params)
      结束

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

      query_s = []
      url_params.each_pair {|键,VAL | query_s.push(#{}键=#{} VAL)}

      如果query_s.size> 0
        @base_link_url +(@ base_link_url_has_qs'和;':'?')+ query_s.join('&安培;')
      其他
        @base_link_url
      结束
    结束
  结束
结束
 

用法:

 #你的看法
will_paginate收集,:渲染=> CustomLinkRenderer,:base_link_url => '/任何你想要的'
 


现在回到你的情况。这个时候,你可能看到的解决方案 - 你可以在一个页面上有不同的基本URL 2 will_paginate部件通过传递不同的:base_link_url 选择这两个

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. 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". I am unsure how to go about solving this problem. Here are some of my code. All suggestions are much appreciated!

Micropost Render 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

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

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). That base url is then combined with any extra params passed to will_paginate helper using :params and succesive page numbers.

For now (will_paginate 3.0.3), in order to overwrite this default behavior, you need to write your custom LinkRenderer class. 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. 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.

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):

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

这篇关于问题will_paginate页面链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆