will_paginate在Web上不显示... [英] will_paginate not showing on the Web...

查看:115
本文介绍了will_paginate在Web上不显示...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前面的问题在这里:
限制RoR结果的常见做法是什么?

以下是users_controller:

Here's the users_controller:

def show
  @user = User.find(params[:id]) 

  @posts = @user.posts.paginate :page => params[:page] 

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @user }
  end
end

和我的show.html.erb:

and my show.html.erb:

<%=h @posts.length %> 
<%=h @posts.inspect %> 

<%= will_paginate @posts %>

但是在浏览器中,我只能得到这个:

But in the browser, I only get this:

4 [#<Post id: 7, title: "I am a root", description: "what can I do", views: 8, created_at: "2010-07-12 15:16:26", updated_at: "2010-07-12 15:16:26", user_id: 32>, #<Post id: 8, title: "root Post two", description: "This is the second one.", views: 2, created_at: "2010-07-12 15:42:57", updated_at: "2010-07-12 15:42:57", user_id: 32>, #<Post id: 9, title: "The third one.", description: "Ok, this is the third ads", views: 3, created_at: "2010-07-12 15:43:18", updated_at: "2010-07-12 15:43:18", user_id: 32>, #<Post id: 10, title: "I type very many", description: "What is very many for?", views: 33, created_at: "2010-07-12 15:43:34", updated_at: "2010-07-12 15:43:34", user_id: 32>]

除此之外,我没有看到其他任何东西。

Besides that, I don't see any other stuff.

我试图用控制台解决问题:

I tried to trouble shooting with console:

>> defined? WillPaginate
=> "constant"
>> [].paginate
=> []
>> ActiveRecord::Base.respond_to? :paginate
=> true


推荐答案

在这种情况下,应该... will_paginate方法只会在必要时打印分页链接。你没有看到帖子列表,因为你不会迭代@posts数组。尝试加入:

In this case, you are seeing exactly what you should... the will_paginate method will only print out pagination links if necessary. You don't see the post listing, because you don't iterate though the @posts array. Try adding this:

<% @posts.each do |post| -%>
  <p>Title: <%= post.title %>
  ...
<% end -%>

你可能不会得到分页链接,除非你在数组中有30条记录(我认为这是默认)。您也可以在控制器中执行此操作,以查看使用较小数据集的分页:

You probably will not get the pagination links unless you have 30 records in the array (I think that is the default). You can also do this in your controller to see the pagination working with a smaller set of data:

@posts = @user.posts.paginate :page => params[:page], :per_page => 2

希望这有助于您!

这篇关于will_paginate在Web上不显示...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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