will_paginate 未显示在网络上...

发布于 2024-09-09 06:33:01 字数 1535 浏览 6 评论 0原文

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

这是 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

The previous question is here :
what is the common practice on limit the result in RoR?

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

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

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

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

发布评论

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

评论(2

公布 2024-09-16 06:33:01

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

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

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

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

希望这会有所帮助!

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

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

Hope this helps!

千里故人稀 2024-09-16 06:33:01

摆脱 inspect 行并添加自定义代码来显示对象的每个属性。

Get rid of the inspect line and add custom code to display each property of your objects.

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