I have data and they be cut on some pages (10 results per page).
code in controller:
#messages = Message.order('id DESC').page params[:page]
How I can show all results on one page if I want? It similar as 'see all' on page navigate.
You can put a very high limit in the per_page option if you still want the paginate helpers to work in your view.
#messages = Message.order('id DESC').page params[:page]
if params[:all]
#messages = #messages.per_page(Message.count) # you can also hardcod' it
end
Related
I'm starting to implement AMP pages in my Rails 5.2 app. On desktop, I'm using the Will Paginate gem.
However, with AMP I'm using the framework's infinite scroll JS, and I don't want to paginate the results.
So in the controller action, I want to determine if the user is viewing an AMP page (the URL ends with .amp) to determine what I have for this line (include paginate or exclude):
#products = #products.order('price DESC, RANDOM()').paginate(page: params[:page], per_page: 24)
How do I do this?
The simple solution is just test request.format. So you could write
if request.format == :amp
# do not paginate
else
# all other formats we paginate
end
But inside a controller, we can also use an explicit respond_to block:
def index
respond_to do |format|
format.amp do
#products = #products.order('price DESC, RANDOM()')
end
format.html do
#products = #products.order('price DESC, RANDOM()').paginate(page: params[:page], per_page: 24)
end
end
end
This will explicitly specify all the formats the controller method accepts and responds to.
As you probably already do, but you will have to provide a view for index.amp and a index.html.
To be clear: this will also block all non-specified formats! E.g. retrieving a index.json will get a 405 Not allowed.
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'm using kaminari gem for pagination. The problem is that I need to paginate on the first page after 4 elements, and on the all other pages after 25 elements. It it possible to configure kaminari to solve my problem?
Here is a usage:
.pagination
.pagination__back
- if params[:page] && params[:page].to_i > 1
= link_to "Previous news", news_items_path(page: params[:page].to_i - 1)
- else
= ""
.pagination__forward
- if params[:page]
= link_to "Next news", news_items_path(page: params[:page].to_i + 1)
- else
= link_to "Next news", news_items_path(page: 2)
First of all you can leave out all the code related to forward/previous pages. Kaminari solves this with its helper already.
For that matter, inside your view, use the following code:
= paginate #your_resource
This will render several ?page=N pagination links surrounded by an HTML5 tag.
(Source)
To have a paginated resource, you want to add the following code to your controller:
#your_resource = YourResource.order(:foobar).page(params[:page])
# params[:page] will get added to each "paginated" request by Kaminari
# if you use its previously mentioned helper method.
Now you want a dynamic limit. For that matter I suggest adding something like this:
def index
#your_resource = YourResource.order(:foobar).page(params[:page]).per(dynamic_limit(params[:page]))
end
private
def dynamic_limit(current_page = 1)
if current_page == 1
return 4
else
return 25
end
end
This way you will check for the current page, and if it's the first page it'll limit the results to 4. Otherwise, 25.
I have a message box using will_paginate.
I have 6 messages showing in 3 pages,
when i delete an item in page_3, it redirect_to page_1, how can i still be in page_3?!
2 ways to solve the issue:
"redirect_to :back"
save params[:page] and redirect to the page once item is deleted
You can use method total_pages on instance variable which you are using for pagination.
example :
#user = User.find(params[:id])
#comments = #user.comments.paginate(:page => 1, :per_page => 20)
#comments.total_pages
#comment.total_pages will return you the last page no. which you can use for your redirection.
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