2010-07-12 55 views
1

前一个问题是在这里: what is the common practice on limit the result in RoR?will_paginate不显示在Web上...

这里的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:

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

<%= will_paginate @posts %> 

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

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>] 

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

我试图麻烦拍摄控制台:

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

回答

6

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

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

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

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

希望这有助于您!

0

摆脱inspect线并添加自定义代码,以显示你的对象的每个属性。