Will_Paginate JSON Support

will_paginate JSON support?

Something in the lines of:

@posts = Post.paginate :page => params[:page]

respond_to do |format|
format.json {
render :json => {
:current_page => @posts.current_page,
:per_page => @posts.per_page,
:total_entries => @posts.total_entries,
:entries => @posts
}
}
end

Once you figure out what format you want, you can always define a to_json method on a WillPaginate::Collection.

will_paginate can it order by day

You cannot use will paginate by group/scope by date. You'll need to do this yourself. It's not terribly difficult though, you could do something like this:

# pictures_controller.rb

def index
@date = Date.parse(params[:date]) rescue Date.today
@pictures = Picture.where(:created_at => @date.at_midnight..@date.next_day.at_midnight)

@total_pictures = Picture.count
@current_pictures = @pictures.size
end

# pictures/index.html.haml

- @pictures.each do |picture|
= #....

= "Showing #{@current_pictures} pictures for #{@date}."
= "There are a total of #{@total_pictures} pictures."

= link_to 'View Previous day photos', pictures_url(:date => @date.prev_day)
- if @date.past?
= link_to 'View Next day photos', pictures_url(:date => @date.next_day)

Add pagination count to the same GET response in Rails API

you can send all 3 commands in a single json response, then all your data is in one object.

@apartments = Apartment.paginate(page: 1, per_page: 20)
render json: {apartments: @apartments,
total_pages: @apartments.total_pages,
total_entries: @apartments.total_entries }

will_paginate not working with arrays

Found the answer by looking at source code in will_paginate/array:

def paginate(options = {})
page = options[:page] || 1
per_page = options[:per_page] || WillPaginate.per_page
total = options[:total_entries] || self.length

WillPaginate::Collection.create(page, per_page, total) do |pager|
pager.replace self[pager.offset, pager.per_page].to_a
end
end

So for arrays, you have to use .paginate (not .page), and you have to pass it as a hash. So the following works:

def index
@array = (1..100).to_a.paginate(page: params[:page])
end

Ruby on Rails will_paginate an array

will_paginate 3.0 is designed to take advantage of the new ActiveRecord::Relation in Rails 3, so it defines paginate only on relations by default. It can still work with an array, but you have to tell rails to require that part.

In a file in your config/initializers (I used will_paginate_array_fix.rb), add this

require 'will_paginate/array'

Then you can use on arrays

my_array.paginate(:page => x, :per_page => y)

Undefined method `paginate'

will_paginate doesn't work like that. paginate is a method of the Location class:

def index
@locations = Location.paginate(page: params[:page], per_page: 10)
respond_to do |format|
format.html
format.json { render json: @locations }
end
end

Moreover, to use will_paginate you should just need to add the line below in your Gemfile, no modification in environment.rb is required:

gem "will_paginate", "~> 3.0.4" 

Pagination issue with jquery DataTables and will_paginate gem on Rails 5.2

I finally found out that the start parameter does not return a page number, but a row number. I updated my code with the following:

# Calculate next page
page_number = (params['start'].to_i / params['length'].to_i) + 1
@child_values = @child_values.datatable_order(params['order']['0']['column'].to_i, params['order']['0']['dir'])
@child_values = @child_values.paginate(page: page_number, per_page: params['length'].to_i)

Rendering paginated pages in JBuilder views

Kaminari works great of the box for HTML partials, but there are some additional things you need to do to set it up for other response formats. You can remove the paginate json: @attendees, per_page: 500 line from your controller in favor of something like

@attendees = @conference.attendees.page(params[:page]).per(500)

Additionally you will need to provide additional information to your jbuilder partial to render this information.

Something like this:

json.meta do
json.total_pages @attendees.total_pages
json.total_count @attendees.total_count
end
# See the Kaminari docs for more methods available https://github.com/kaminari/kaminari#the-page-scope


Related Topics



Leave a reply



Submit