Ruby on Rails: Show default result before using dropdown filter with will_paginate - ruby-on-rails

I can display the results by 10, 15, or 20 by using a drop-down. The issue is when the app loads it displays all results. I would like to display only 10 results by default before using the drop-down filter to display more results.
Can someone help please?
Thank you!
This is my VIEW:
<%= select_tag :per_page, options_for_select([10,15,20], #per_page), :onchange => "if(this.value){window.location='?per_page='+this.value;}" %>
This is my CONTROLLER:
#per_page = params[:per_page] || Post.per_page
#posts= Post.all.paginate(:per_page => #per_page, :page => params[:page])

Try this code:
#per_page = params[:per_page] || Post.per_page
#posts= Post.paginate(per_page: #per_page, page: params[:page])
Note that I call paginate without on Post class itself and not on result of Post.all.
I also removed the last || 10 part. I think you don't need it there as you already set this value on Post model.

Related

rails 4 will_paginate set count of links on pages

I'm using rails 4 and gem will_paginate. I can't customize count of links on pages before gap (...). eg now I having 9 pages than ellipsis and 3 last pages. How can I set 5 pages instead 9 (see scrennshots)?
In controller:
#per_page = params[:per_page] || 6
#events = Event.not_cancelled.paginate(:page => params[:page], :per_page => #per_page)
Now
That I want
You must change value for inner_window. Reference here.
So here is code:
<%= will_paginate #events, :inner_window => 2, :outer_window => 1 %>

Getting total result count from Rails query using will_paginate

I get a list of objects from my Rails app, and use will_paginate to page as usual, and then I have a little method used to save details of the search to the database:
per_page=10
session[:search_params] = params[:search_people]
#documents = Person.search_people(params[:search_people], params[:page], per_page)
Search.create(:user_id => (!current_user ? 0 : current_user.id),
:search_type => "Person",
:firstname => params[:search_people][:first_name],
:lastname => params[:search_people][:last_name],
:results => #documents.count )
The problem is, the number of search results (#douments.count) is always <= per_page used for will_paginate.
I understand why this is, but is there a way around it without running the query twice, once with will_paginate and once without?
Try <%=#documents.total_entries%>

Change pagination dynamically in ActiveAdmin or solutions for printing

I'm new to Activeadmin and rails and I need some help.
I have a model that is paginated and I want to allow the user to change the pagination value or disable it completely, so it can print (to a printer) all the records (or filtered ones) for instance.
I know I can set the pagination using #per_page in :before_filter, but I can't figure out how I can change this value during execution.
To solve the problem of needing to show all the unpaginated records I defined a custom page, but in this page the filter or scope don't work so it's kind of useless.
How can I create a Print button in Active Admin?
This is a workaround to do it, I know it is not the best solution but it works ! :)
This is the app/admin/mymodel.rb file
ActiveAdmin.register MyModel do
before_filter :paginate
#other code
controller do
def paginate
#per_page = params[:pagination] unless params[:pagination].blank?
end
end
index do
panel "Pagination" do
render partial: "paginate", locals: {resource: "mymodels"}
end
#other code
end
#other code
end
And for the app/views/admin/articles/paginate.html.haml
#pagination_form
= form_tag do
= label_tag :pagination, "Number of " + resource + " per page : "
= text_field_tag :pagination
= submit_tag "Filter"
:javascript
$("#pagination_form form").submit(function(e){
e.preventDefault();
window.location = "/admin/#{resource}?pagination=" + $("#pagination").val();
})
Hoping that my answer can people with the same problem :)
I found a solution and I'm answering my own question for someone who has the same problem.
It may not be the best solution but it works, if someone has a better way please share:
ActiveAdmin.register mymodel do
before_filter :apply_pagination
# other code
index :download_links => false, :as => :table, :default => true do
if params[:pag].blank?
div link_to(I18n.t("text_for_the_link"), 'mymodel?pag=1', :class => "class_for_link")
else
div link_to(I18n.t("print.print"), 'mymodel', :class => "class_for_link")
end
# other code
end
controller do
def apply_pagination
if params[:pag].blank?
#per_page = 50
else
#per_page = 99999999
end
# other code
end
end
I found out you can define this by registering the following line on the resource:
ActiveAdmin.register MyModel do
config.per_page = [20, 50, 100, 200]
end
It automatically adds a select box in the index pagination with the preset values given in the array.

Paginating a solr response using kaminari?

Does kaminari support pagination of a solr response ? If yes, how to convert the response to a kaminari-compatible format ?
Set up Kaminari for your project as described here - http://railscasts.com/episodes/254-pagination-with-kaminari
In your controller, if you have the following to perform the Solr search -
#vehicles = Vehicles.search do
with (:year => 2012)
paginate :page => params[:page], :per_page => 20
end
You need to add the following in your view to get the pagination links -
<%= paginate #vehicles.hits %>
Hope that helps!

Can't sort table in rails

I'm having trouble sorting a single-column table in Rails. Each row represents a single object (an article) and contains all of its attributes (name, content, created_at, user, etc.). The search function works fine (Article.where) but I can't seem to sort the table by any attributes, i.e. Article.order('attribute'). The default, which I can't change, is created_at desc. Am I overlooking something?
Here is my controller:
def index
#title="Home"
if params[:search]
#search=params[:search]
#articles=Article.where('name LIKE ? OR category LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%").paginate(:per_page => 15, :page => params[:page])
else
#articles=Article.order('name').paginate(:per_page => 15, :page => params[:page])
end
end
And view:
<table>
<%= render #articles%>
</table>
<%= will_paginate #articles, :previous_label => "Prev", :next_label => "Next" %>
Use reorder to override any default ordering.
Article.reorder('name').paginate(:per_page => 15, :page => params[:page])
I recommend my gem simple-search for these problems. It may be too simple, but worth a shot.

Resources