I am using will_paginate and sort helper for pagination and sorting resp..
But im facing one problem while sorting that when im on page 2 or 3 or any other page than first page.
It redirects to the first page and sorts the first page only.
please help me how to sort all the records and go back to page where I was.
What's the reason you need to go back to the page (2 or 3)? Records will change position and probabily page so you will not find them at the same place.
so why you don't change only the :order value (ex. from 'firstname DESC' to 'lastname DESC) when you call the action again?
# people_controller.rb
def index
#people = People.search(params[:my_order], params[:page])
end
# models/people.rb
def self.search(my_order, page)
paginate :per_page => 10, :page => page,
:conditions => ['job like ?', "Driver"],
:order => "%#{my_order}%"
end
I have more powerful solution for this: https://github.com/mynameisrufus/sorted
Related
Im building a web application in rails to fetch records from a third party API. This third party API accepts page parameter. For eg: GET http://thirdpartyapi.com/records?page=2
How can I build my html in paginated format, so that when user clicks on number 2, it should send page=2 and when user clicks on number 4, it should send page=4 in the requests. Is there any gem for that?
class DemoController < ApplicationController
def index
response = HTTP.get('http://thirdparty.com/records', {query: {page: params[:page]}}) # it will return 30 items by default
#items = response['items'].paginate(page: params[:page], per_page: 10)
end
end
This is my views
<%= will_paginate %>
If you want to do this manually it's more work than just using the will_paginate gem
First you would need to get a count of records so that you know how many pages (aka how many links at the bottom you will have for pages)
#num_of_records = Object.count / per_page
Then you will need to handle that through JS depending on the number of links you want to display.
Once a user clicks on yourlink.com/?page=2 it should load with the correct data, or if you choose to you can remove elements from the div/table and insert new ones by returning them if you do an AJAX call.
Your controller would look something like this:
def index
page = params[:page] || 1
per_page = 10
#num_of_records = Object.count / per_page
objects_to_append = Object.paginate(:page => page, :per_page => perPage)
render json: { success: true, objects_to_append: objects_to_append }
end
I highly recommend to use kaminari instead. they have a way to do this easily.
https://github.com/kaminari/kaminari#paginating-a-generic-array-object
I have a Rails application which uses AngularJS for displaying and asynchronously updating a list of objects (configured as AngularJS resources). Is there a simple way to server-side paginate this table?
The will_paginate gem offers a way to paginate queries in Rails controllers and models. From the github README:
## perform a paginated query:
#posts = Post.paginate(:page => params[:page])
# or, use an explicit "per page" limit:
Post.paginate(:page => params[:page], :per_page => 30)
## render page links in the view:
<%= will_paginate #posts %>
You can customize the default page contents:
# for the Post model
class Post
self.per_page = 10
end
# set per_page globally
WillPaginate.per_page = 10
And with Active Record 3 you can do things like:
# paginate in Active Record now returns a Relation
Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')
# the new, shorter page() method
Post.page(params[:page]).order('created_at DESC')
Try doing pagination server side with #ngTasty
git : https://github.com/zizzamia/ng-tasty
docs : http://zizzamia.com/ng-tasty/directive/table-server-side
I'm trying to integrate Tire into my site and I'm having difficulty with pagination. I've tried paginating the results outside of the context of Tire and will_paginate is working on that Array. However, when I try will_paginate within the context of Tire I'm having one large problem.
Will_Paginate will display the correct number of pages with consideration of :per_page but when I click on that page the results are not loaded, rather they are the same as on the first page. The page number is highlighted in the will_paginate navigation.
#results.inspect yields this:
#<Tire::Search::Search:0x007f88ab9153d0 #indices=["deja-set-development"], #types=[], #options={:load=>true, :page=>1, :per_page=>2}, #path="/deja-set-development/_search", #query=#<Tire::Search::Query:0x007f88ab915088 #value={:query_string=>{:query=>"oh"}}>, #facets={"type"=>{:terms=>{:field=>:_type, :size=>10, :all_terms=>false}}}>
Here is where I call will_paginate:
= will_paginate #search_results.results, params
Here is where I iterate through the results
#search_results.results.each
Does anyone have any thoughts?
Edit ---
I'm not sure what is going on, but I did this and it is working.
#search_results = #search_results.paginate(:page => params[:page], :per_page => 5)
Please see the integration test in Tire, and make sure you're passing all options properly.
So to clarify, I've attached my github correspondence with #karmi here.
https://github.com/karmi/tire/issues/627#issuecomment-13449368
I was using Tire.search as opposed to searching by model. As #karmi notes, at the moment :per_page and :page are not supported with Tire.
Here is how I solved this:
#search_results = Tire.search [:index1, :index2, :index3], :load => true, :from => from, :size => size do
query do
string q, :default_operator => 'AND', :fields => [:name1, :name2]
end
end
I ended up having to spin my own small pagination system to increment 'size' and 'from'. Here's the elasticsearch link on the topic.
http://www.elasticsearch.org/guide/reference/api/search/from-size.html
You're able to still access
= #search_results.results.total_entries/next_page/previous_page
which helps with pagination.
Thank you again #karmi.
I am currently developing a Ruby on Rails blog. I have my blog posts show up on the main page, however, I would like to list the posts 5 at a time, so that my frontpage doesn't go on forever and my blog will look much cleaner.
Let me know if you can help. Much appreciated.
Looks like you need a pagination solution - consider using kaminari or will_paginate ( https://github.com/amatsuda/kaminari, https://github.com/mislav/will_paginate/wiki )
And if you need an endless page, there is a nice screencast about that: http://railscasts.com/episodes/114-endless-page
For example, if using will_paginate for pagination, you just call paginate method at end of line your query inside controller, for example inside your controller
def index
#blogs = Blog.all.paginate(:page => params[:page], :per_page => 5)
end
from your view, just simply put:
will_paginate #blogs
at specify location, to show pagination.
If I understand right, you want to limit the number of post on the home page . Then you should do like
Model.find(:all, :limit => 5, :order=> 'created_at desc')
you can remove the order if you don't need it. If you need to make pagination take a look at will_paginate
I'm using the latest version of will_paginate with rails 3. I'd like to use out_of_bounds? to set the current page to the last page if the page parameter is higher than the last page. I found a way to do it like this:
people = People.all.paginate :page => params[:page], :per_page => 20
#people = people.paginate(:page => (people.out_of_bounds? ? people.total_pages : params[:page))
The problem with this is that I have to call paginate twice. The first time to create a WillPaginate::Collection in order to use the out_of_bounds? and total_pages methods, and the second time to actually set the current page. I also need to do this with more actions so it's getting kinda out of hand. I can't use a before_filter or an after_filter, either (I don't think?). Is there a better way to achieve this?
I don't know the best solution, but at least you can do the check before the second call to the method (I suppose that in most of the cases, the page parameter will be ok, so you wont have to call paginate twice):
people = People.all.paginate :page => params[:page], :per_page => 20
#people = people.out_of_bounds? ? people.paginate(:page => people.total_pages) : people