Search code examples
ruby-on-railsjsonruby-on-rails-3.2ruby-on-rails-3.1will-paginate

will_paginate render all the values if page and per_page is not sent


I use will_paginate to get the values in paging. consider the example

if params[:page] || params[:per_page]
    @users = User.paginate(:page => params[:page], :per_page 
    => params[:per_page]).all(:order => 'id ASC')
else
    @users = User.all(:order => 'id ASC')
end

format.json {
 render json: 
      {:users => @users, :current_page => @users.current_page, 
       :total_pages => @users.total_pages, 
       :total_entries => @users.total_entries
      }
}

if i did not send any parameter( params[:page] or params[:per_page] ) then it shows

NoMethodError (undefined method `current_page' for #<Array:0x000002345ab655>)

Solution

  • Try:

    User.paginate(:page => params[:page], :per_page => User.count)
    

    NOTE:

    It will put all the users in one page and thus it will obstruct the significance of
    Pagination. But if this is all you want, it is for you.