Change number of elements per page with <%= will_paginate %> - ruby-on-rails

I'm using the will_paginate gem. The default is 30 elements per page. How do I customize this?

If your controller is called User, you can do something like this in your controller:
#users = User.paginate :page => params[:page], :per_page => 10, :order => 'name ASC' This will show 10 results per page.
In your view:
<%= will_paginate #users %>

See the per_page option here:
https://github.com/mislav/will_paginate/wiki
It will allow you to change the number displayed per page, for anytime that model is paginated.
For a controller/action specific approach see Raunak's answer.

Related

User-changeable pagination preferably using will_paginate gem

I am using the will_paginate gem and am trying to figure out how to make it so that the user browsing the site can change how many items he/she wants per page using a list, like the list on the right side of this picture
(random google image search).
I'm also new to rails so I would very much appreciate it if you could give me an idea as to where each code would go. Thanks for any help.
in Gemfile
gem 'will_paginate'
in your controller where you want to paginate
class SomeController < ApplicationController
def index
#somethings = Model.all.paginate(page: params[:page], per_page: params[:per_page])
end
end
in your view
<%= select_tag :per_page, options_for_select([5,10,20,50],params[:per_page].to_i),
:onchange => "if(this.value){window.location='?per_page='+this.value;}" %>
With will_paginate you would simply use the per_page option:
#posts = Post.paginate(
page: params[:page],
per_page: params[:per_page]
)
However you should be wary when taking user input and passing it to a SQL query like this. In this case it it prudent to cast the input to an integer:
irb(main):008:0> nil.to_i
=> 0
irb(main):009:0> "gobeligook".to_i
=> 0
per_page = params[:per_page].to_i.zero? ? params[:per_page].to_i : 30
#posts = Post.paginate(
page: params[:page],
per_page: per_page
)

How to do pagination across multiple tabs using Kaminari Gem

I have a method which fetches results using solr. for eg.
def test
#result1 = Model1.search do
fulltext params[:search]
paginate page: params[:page], per_page: 12
end
#result2 = Model2.search do
fulltext params[:search]
paginate page: params[:page], per_page: 12
end
end
Now I have 2 results and I am displaying this in test page across 2 tabs.
When I display content via tabs, the data gets populated correctly in each tab, but I have a problem in pagination.
When I click suppose say on page number 3 in result2 tab it displays page3 of result2 but when I click on result1 tab it takes me to page3 of result1 tab. Where as it should go to page1 when I click on the result1 tab.
<%= paginate #result1 ,:params => { :anchor => 'result1' }%>
<%= paginate #result2 ,:params => { :anchor => 'result2' }%>
How can I solve this. I am using kaminari gem for pagination.
You can use the option param_name in kaminari like so
<%= paginate #result1, param_name: 'result1' %>
and likewise for #result2. Then instead of params[:anchor] you should use params[:result1] to get the page number. Since you'll be using different param names for #result1 and #result2, they won't interfere with each other

How to make partial_counter work with pagination

I have a blogging application in which User has_many posts. I am using pagination with Booststrap. How can I make the partial_count method work with pagination? Currently, the count resets on every page instead of carrying over across pages.
posts_controller.rb
def index
#posts = Post.order("created_at desc").paginate(page: params[:page], :per_page => 12)
end
views/posts/index.html.erb
<%= render #posts %>
<%= will_paginate %>
views/posts/_post.html.erb
<%= post_counter +1%>
<%= post.name %>
The counter works fine on the first page. However, all subsequent pages also start with "1". How can I make subsequent pages start with (number of pages * 12 + 1) instead?
Thanks for your feedback!
Use #posts.offset to get the proper counter initialisation.

Paginate Multiple Models in Kaminari

I'm creating a search page that will do an application wide search on users, posts, and comments. I currently have:
# POST /search
def index
query = params[:query]
#users = User.search(query).page(params[:page])
#posts = Post.search(query).page(params[:page])
#comments = Comment.search(query).page(params[:page])
respond_to do |format|
format.html
end
end
However I'm really trying to get something where all the results are mixed together then paginated. What are some of the strategies for doing paginated search like this? Thanks!
Ever since this commit: https://github.com/amatsuda/kaminari/commit/f9f529fb68ab89feea38773a4c625c1b14859128
You can do the following
In your view you can do this:
<%= paginate #users, :remote => true, :param_name => "user_page" %>
<%= paginate #posts, :remote => true, :param_name => "post_page" %>
<%= paginate #comments, :remote => true, :param_name => "comment_#{some_post_id}_page" %>
and then in your controller you can refer to them in this way:
#users = User.search(query).page(params[:user_page])
#posts = Post.search(query).page(params[:post_page])
#comments = Comment.search(query).page(params[:comment_page])
and your view's js.erb you might have something like:
$('#posts').html('<%= escape_javascript render(#posts) %>');
$('.table-pager').html('<%= escape_javascript(paginate(#posts, :remote => true).to_s) %>');
Before thinking about a solution, you need to first define exactly what you want the final result to be. If you want to display a few of each type of record on the results page you can modify the approach you posted and combine the three paginated results using:
#results = #users + #posts + #comments
#results.sort! { |a, b| a.score(query) > b.score(query) }
Each object will need to have an instance method 'score' that will let it sort based on the query priority. Also, you will need to modify your view to handle correct rendering of each item and ensure that the pagination is called on the model with the most pages.
Alternatively, a more robust method would be to add a full-text search service (such as Index Tank, Web Solr, Thinking Sphinx). The technology for what's hot for these moves quickly, so do some research and find one that fits your needs. Example syntax for this would be something like:
User.multi_solr_search query, models: [Post, Comment]
You could combine the results from the query and run page on that.
users = User.search(query)
posts = Post.search(query)
comments = Comment.search(query)
#results = users + posts + comments
#results.page(params[:page])

Using Websolr with Heroku to do full text search

Hi I am having trouble trying to figure out how to implement a search form globally across my application. I have a series of posts that need to be searchable by users that are signed in and not signed in. I have added this code in my post model:
searchable do
text :content, :default_boost => 2
text :body, :default_boost => 1.5
end
However, I do not know where to go from there to create a search field across all pages and make it show the results I need. I am new to rails and would be happy to post more information if someone is willing to help me out.
First, you should add your search field like explained in this railscast: http://railscasts.com/episodes/37-simple-search-form
Since your search isn't specific to a particular model, use a generic controller name instead of ProjectsController though.
Then, you should replace the ActiveRecord finder by the use of the Sunspot DSL.
Here is an sample code to help get you started:
page = #page = params[:page] && params[:page].to_i || 1
#search = Sunspot.search(Realty) do # search_ids
per_page = params[:per_page] && params[:per_page].to_i || 10
# not adapted to your case
with(:equipments).all_of params['equip'].split(' ') if params['equip']
case params[:sort]
when "average_rating"
order_by :average_rating, :desc
when "type"
order_by :type, :asc
end
paginate :page => page, :per_page => per_page
# other criteria...
end
In your view, you can then iterate through #search.results
<%= will_paginate #search.results %>
<% #search.results.each do |hit| %>
<%# 'path' contains the stored polymorphic_path of each model object #%>
<% link_to hit.stored('path') do %>
<p><%= hit.stored('content') %></p>
<% end %>
<% end %>
Last, using WebSolR instead of a standard SolR server is quite simple, you can follow the setup instructions at https://github.com/onemorecloud/websolr-rails.
Edit:
As Nick commented, you should totally go to http://docs.heroku.com/websolr.
Thanks Nick !

Resources