ruby-on-rails - will_paginate 未在网络上显示...

标签 ruby-on-rails ruby rubygems will-paginate

上一个问题在这里: what is the common practice on limit the result in 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

最佳答案

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

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

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

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

希望这对您有所帮助!

关于ruby-on-rails - will_paginate 未在网络上显示...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3230344/

相关文章:

javascript - 如何将错误消息关联到 json 响应中的 'error' 键

ruby-on-rails - Rails 是否缓存模板?

ruby - 挂载多个 Rack 应用程序,无需在 url 中添加前缀

ruby - 如何在 HAML 中包装多行 Ruby 参数?

ruby-on-rails - Ruby on Rails : How to run a background task automatically when the server starts?

ruby-on-rails - Rails 2 应用程序 + RubyGems 1.8.7 失败

mysql - 如何查找特定时间之后的所有数据库行

ruby-on-rails - 如何在 Rspec 3 功能规范中 stub Rails 帮助器/模块方法?

ruby-on-rails - Ruby DateTime.new 困惑

ruby-on-rails - 如何从 gem 路径中删除项目?