will_paginate undefined method `total_pages'

后端 未结 5 1928
失恋的感觉
失恋的感觉 2020-12-13 12:36

What am I missing here? I am using the haml_scaffold generator and the pages work fine with will_paginate. When I start tinkering I end up with this \'total_pages\' error an

相关标签:
5条回答
  • 2020-12-13 13:06
    @locations = Location.paginate(:all, :conditions => 'city = springfield')
    

    @locations must be an object

    in your example @locations is an Array

    array cant have total_pages method

    0 讨论(0)
  • 2020-12-13 13:07

    If anyone else is having this problem. In my case I did not call the pagination in my controller method last.

    Example Before:

    @foo = Foo.paginate(:page => params[:page], :per_page => 15)
    @foo = Foo.do something
    

    Example After: I called the pagination last within my method since rails reads from top to bottom and it seemed to fix my error.

    @foo = Foo.do something
    @foo = Foo.paginate(:page => params[:page], :per_page => 15)
    
    0 讨论(0)
  • 2020-12-13 13:09

    include

    require 'will_paginate/array' 
    

    before loading that code will solve your problem.

    0 讨论(0)
  • 2020-12-13 13:25

    I was encountering a similar a error undefined method 'total_pages' for ActiveRecord_AssociationRelation.

    It turns out that renaming my variable solved the issue.

    Here was my code:

    @reviews = @user.reviews.paginate(page: params[:page], per_page: 5).order('created_at DESC')
    

    I changed it to this:

    @user_reviews = @user.reviews.paginate(page: params[:page], per_page: 5).order('created_at DESC')
    

    And it solved the issue. It's very bizarre but it there may have been some kind of conflict.

    Since I spent a few hours researching, I thought it might help someone one day as well!

    0 讨论(0)
  • 2020-12-13 13:28

    try to change

    @locations.paginate(:page => 1, :per_page => 2)
    

    to

    @locations = @locations.paginate(:page => 1, :per_page => 2)
    

    hope this helps

    0 讨论(0)
提交回复
热议问题