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
)
Related
I am creating Rails API and when I do get request to mywebsite.com/posts/all it shows me all the post data. I am trying to do mywebsite.com/posts/all?limit=20 to show 20 results or some other number. What is the way to achieve it with rails?
Limit will not work, each time if u call this method it will return only last 20 records,
Best way to achieve this result using pagination, here you can limit number record passing when each request is hitting
You can use gem will_paginate
In controller
#posts = Post.paginate(:page => params[:page], :per_page => 20)
In the view
<%= will_paginate #posts %>
For info about will_paginate
https://github.com/mislav/will_paginate/blob/master/README.md
If u r using API pass params ass below
GET /v1/posts?page=1&per_page=10
Change value in controller
#posts = Post.paginate(page: params[:page], per_page: params[:per_page])
Easy way for this is
#posts = Post.limit(params[:limit])
But you could also use will_paginate gem
I am paginating an array #items
Here's the relevant controller part
class StaticPagesController < ApplicationController
def home
if signed_in?
#post = current_user.microposts.build
#activities = PublicActivity::Activity.order("created_at desc").paginate(page: params[:page])
#feed_items = current_user.feed.paginate(page: params[:page])
#items = (#feed_items + #activities)
#items.sort_by! {|item| -item.created_at.to_i}
#items = #items.paginate(:page => 1, :per_page => 10)
else
redirect_to '/signin'
end
end
Basically, this is the line that I'm using.
#items = #items.paginate(:page => 1, :per_page => 10)
I also tried changing that to the code below but didn't work.
#items = #items.paginate(page: params[:page], :per_page => 10)
Inside initializers\will_paginate_array_fix.rb
I have this line
require 'will_paginate/array'
In my view, I am using this
<%= render partial: 'shared/item', collection: #items %>
<%= will_paginate #items %>
It seems to work fine for the first page, but when I click on page 2 or 3 or others, I get a blank. It's null. Anyone know how I can fix this?
I think your problem is that you are building your array by combining activities and feed items, which are already paginated and then your are paginating it again.
For example assume that your activity and feed item models are using the will paginating default which is something like 20 items per page and that there are 15 activities and 12 feed items.
On page 1 you combine this array,take the top 10 and display them. #items had 27 entries so you show links to page 2 & 3. But when you click on that you're trying to load page 2 of activities and page 2 of feed items, which are empty since there are fewer than 20 of each.
In my experience trying to paginating through several collections like this is very tricky (unless you can load all of them and just paginate the array). A "load more items" button is easier because you just have to keep track of what the last cutoff date was
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.
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.
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 !