Kaminari:未定义的方法total_pages

时间:2016-03-17 08:42:43

标签: ruby-on-rails-4 kaminari

我在使用Rails 4的Kaminari上遇到了问题。

这是我的renters_controller.rb

def search
  @location = params[:search]
  @distance = params[:km]
  @renters = Renter.near(@location, 30000).order("distance")
  @renters = @renters.page(params[:page])

  if @renters.empty?
    @renters = Renter.all
    search_map(@renters)
  else
    search_map(@renters)
  end
end

和我的观点search.html.haml。

.row
  = paginate @renters

和我的错误消息

Started GET "/search?utf8=%E2%9C%93&search=" for ::1 at 2016-03-17 09:37:59 +0100
Processing by RentersController#search as HTML
Parameters: {"utf8"=>"✓", "search"=>""}
(0.5ms)  SELECT COUNT(count_column) FROM (SELECT  1 AS count_column FROM "renters" WHERE (false) LIMIT 25 OFFSET 0) subquery_for_count
Renter Load (0.9ms)  SELECT "renters".* FROM "renters"
Rendered renters/search.html.haml within layouts/application (8.2ms)
Completed 500 Internal Server Error in 23ms (ActiveRecord: 1.5ms)

ActionView::Template::Error (undefined method `total_pages' for #<Renter::ActiveRecord_Relation:0x007fd4d36bdc30>):
1: .row
2:   = paginate @renters
app/views/renters/search.html.haml:2:in `_app_views_renters_search_html_haml__3667517883215455359_70276042599600'

在控制器中,如果我写

,它就有效
@renters = Renter.page(params[:page])

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

试试这个,我希望这会奏效。

def search
  @location = params[:search]
  @distance = params[:km]
  @renters = Renter.near(@location, 30000).order("distance")

  if @renters.empty?
    @renters = Renter.all
    search_map(@renters)
  else
    search_map(@renters)
  end
  @renters = @renters.page(params[:page])
end