无法将nil转换为数组will_paginate

Mic*_*ael 6 ruby-on-rails

你好伙计我在rails 3中遇到了will_paginate的问题,我在我的constroller里有这个

@posts = Post.paginate(:all,
  :include => [:author, :categories],
  :conditions=>"status = 'published'", 
  :order=>"blog_posts.created_at DESC",
  :per_page => 1, :page => params[:page])
Run Code Online (Sandbox Code Playgroud)

和@%= will_paginate @posts%>在我的视图中,但我得到此错误无法将nil不断转换为数组.

谁能帮我 ?

提前致谢

Dou*_*rer 11

您需要安装will_paginate版本3.0.pre2才能在数据库级别而不是在阵列上进行分页工作.

gem 'will_paginate', '3.0.pre2'
Run Code Online (Sandbox Code Playgroud)


iai*_*ain 8

试着这样做:

@posts = Post.includes(:author, :categories).where(:status => "published").order("blog_posts.created_at DESC").paginate(:per_page => 1, :page => params[:page])
Run Code Online (Sandbox Code Playgroud)

这是Rails 3的首选方式.

  • 它不应该; 它仍然是一个集合,因此它应该在查询执行之前将其添加到查询中. (2认同)