Limiting Number of Posts Per Page - Ruby on Rails Blog - ruby-on-rails

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

Related

Tire + Will_Paginate not taking Tire.options

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.

Lazy load in RoR App (paginate results)

I want to realize some kind of lazy loading in my rails app. That means I have a query result of about 50 000 lines. I want them to be shown on 50 pages with 100 lines each.
I can't load all the data before because it makes my browser crashing down.
So how do I just load a part of my data at a time?
Does anyone can tell me some keywords that I can google?!
Thanks in advance!
Try the will_paginate gem for this functionality: https://github.com/mislav/will_paginate
It allows you to do things like #posts = Post.order('created_at DESC').paginate(:page => params[:page]) in the controller and <%= will_paginate #posts %> in the view.

What's a better way to use helper methods with 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

Rails pagination of subset of a class

I have a rails app and I'm trying to set up pagination for a view of the Essays class. I'm new to rails... so I can do this for ALL of them, but I only want certain ones to be in this list (where all the essays are contained in Essay.find(Ranking.where(:user_id=>current_user.id).essay_id)).
home.html.erb contains (among other things):
`<%= will_paginate #essays%>
<ul class="users">
<%= render #essays %>
</ul>
<%= will_paginate #essays%>`
in the Pages Controller:
def home
#...
#essays = Essay.paginate(:page => params[:page])
end
I tried adding #essays=Essay.find(Ranking.where(:user_id=>current_user.id).essay_id) before the #essays=Essay.paginate(:page => params[:page]) but the method essay_id for the Ranking class is not available here. How do I get around this? Thanks!
This should work:
Essay.joins(:rankings)
.where(:rankings => {:user_id => current_user.id})
.paginate(:page => params[:page])
While this can be done with will_paginate. I've had some issues with this plugin for Rails 3. A much smoother solution, in my opinion, was to use another pagination plugin called Kaminari.
Assuming, essay_id is passed as a param, you could try:
#ranking = Ranking.where(:user_id => current_user.id, :essay_id => params[:essay_id]).page(params[:page]).per(10)
Or depending on your logic. If the essay object has already been identified in your controller:
#essay = Essay.find(1234)
#ranking = Ranking.where(:user_id => current_user.id, :essay_id => #essay.id).page(params[:page]).per(10)
And then, in your view:
<%= paginate #ranking %>
Even better, to get you started on Kaminari, you can view this rails cast. As noted from the rails cast:
The first-choice gem for pagination in
Rails is will_paginate, but the
currently released version doesn’t
support Rails 3. There is a
pre-release version available that
works but it hasn’t been updated for
several months. If will_paginate is no
longer in active development are there
any other gems we could use?
One alternative is Kaminari. This
seems to provide a cleaner
implementation of pagination and
offers several improved features, too,
so let’s try it in our application
instead.
Hope that helps!
Simply chain paginate method after find method:
#essays=Essay.find(Ranking.where(:user_id=>current_user.id).essay_id).paginate(:page => params[:page])

Pagination and table sorting in Rails

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

Resources