Paginate Comments With will_paginate - ruby-on-rails

I'm using the will_paginate gem and currently have a list that is already paginated.
#businesses = Business.paginate(:page => params[:page], :per_page => 7)
<%= will_paginate #businesses %>
<% #businesses.each do |business| %>
To display the comments for a particular entry I use:
<% #business.comments.each do |comment| %>
I thought something like the following might work but it didn't:
#business.comments = Business.comments.paginate(:page => params[:page], :per_page => 7)
<%= will_paginate #business.comments %>
I can't seem to find any answer to paginating the comments (99.9% sure its to do with it being #something.something instead of just #something)

Setting #business.comments not only won't work but is actually dangerous and destructive. You are setting the comments associated to the business to be only the ones returned by that paginate call. (Check what the log shows when you set #business.comments to the paginated call.
You have, however, got the call right. You just need to set it to a different variable rather than reusing #business.comments. If you use #comments instead it will work fine:
#comments = #business.comments.paginate(:page => params[:page], :per_page => 7)
and
#comments.each do |comment|
...
end
will_paginate #comments

Related

How to display two differents models in my view with will_paginate in rails?

Hi everyone I have a question , I looking for on google but I don't find an answer.
I'm actually want to display Post model and User model by search in same page and for this I want to use Will_paginate to display all of them .
I try this solution =>
**require 'will_paginate/array'
#posts =Post.search(params[:q]).order("created_at DESC")
#users=User.search(params[:q]).order("created_at DESC")
#all_records = (#posts + #users).paginate(:page =>params[:page], :per_page => 10)**
And In my view I want to display all query found ,so I try something like this =>
**<%unless #posts.nil?%>
<% #posts.each do |post| %>
....
**
**<% unless #users.nil?%>
<% #users.each do |user| %>
...**
<%= will_paginate #all_records %>
But it doesn't work nothing happend, so I want to know how to get #posts and #users records in #all_records to do something like this =>
**
<% #all_records[#posts].each do.. %>
<% #all_records[#usets].each do ..%>
**
I saw some solutions that suggest to use two differentes paginate in my view like =>
**<%= will_paginate #posts %>
<%= will_paginate #users %>**
**But It's not want I want , I would'like only one paginate for all, Please Help me cause I'm going crazy, Thank you **

How to show pagination counter for array for pagination in views

used_cars = UsedCar.paginate(:page => params[:page], :per_page => 5)
used_cars_hash = used_cars.as_json
I show all used_cars_hash in my view. But when I add <% will_paginate #user_cars_hash %>
it gives an error
How to show pagination counter on array?
Because when we add <% will_paginate #user_cars_hash %>
it gives an error
undefined method total_pages' for #<Array:0x007f1edef43c48>
will_paginate does not work with the plain Ruby array you get by calling used_cars.as_json. Try passing used_cars to it:
# no need to turn it into a plain array
#used_cars = UsedCar.paginate(:page => params[:page], :per_page => 5)
# in the view:
<%= will_paginate #used_cars %>
If you absolutely have to use the array for pagination, you can do this:
require 'will_paginate/array'
# you can call `paginate` on plain arrays
used_cars = UsedCar.all
#used_cars_array = used_cars.as_json.paginate(:page => params[:page], :per_page => 5)
# in the view:
<%= will_paginate #used_cars_array %>

Rails 4 will_paginate articles/post by tags

trying to paginate a list of articles (ideas in my case) which are on my tag show page. So I am listing all ideas that are tagged with "loremipsum". The problem is :per_page => 3 doesn't seem to take effect. All ideas show up (I have 4 for tag "loremipsum") withour error. Pagination links also show at page bottom (but page2 doesn't work).
tags_controller:
def show
#tag = Tag.find_by_name(params[:id])
#ideas = #tag.ideas.all.paginate(:per_page => 3, :page => params[:page])
end
In show.html.erb
<% #tag.ideas.each do |idea| %>....<% end %>
<%= will_paginate #ideas %>
On my idea list page pagination works just fine so no clue.
Any help much appreciated. Thanks in advance!
I would suggest using #ideas.each in your view rather than #tag.ideas.each.
<% #ideas.each do |idea| %> ... <% end %>
<%= will_paginate #ideas %>
That is, I believe the problem you encountered was because you were not iterating over the same collection that you applied the pagination to. Though #ideas will paginate, #tag.ideas will not.
Replace this line
#ideas = #tag.ideas.all.paginate(:per_page => 3, :page => params[:page])
with
#ideas = #tag.ideas.paginate(:per_page => 3, :page => params[:page])
I believe you don't need that all.

Pagination links not showing on arrays

My app fetches the covers to each of their Facebook photo albums (I do this with Koala), and I'm trying to paginate the results with will_paginate. I have pagination working on other elements, but it doesn't seem to like working with arrays.
I've gone through the documentation, I've used require 'will_paginate/array', and I can get it to limit the amount of entries per page - I have seven albums, and will_paginate will display the first four with the below code:
albums = graph.get_connections("me", "albums")
#albums = albums.paginate(:page => params[:page], :per_page => 4)
But dropping <% will_paginate #albums %> anywhere into the view doesn't seem to affect anything, and certainly doesn't generate the pagination links. Is there something I'm missing/doing wrong?
Use <%= %> Instead of <% %>
i.e
Change
<% will_paginate #albums %>
To
<%= will_paginate #albums %>

Anyone know how to turn off will_paginate in Spree?

Deleting the lines of
<% if paginated_products.is_a?(WillPaginate::Collection)
params.delete(:search)
params.delete(:taxon)
%><div class="clear"></div><%= will_paginate(paginated_products,
:previous_label => "« #{t('previous')}",
:next_label => "#{t('next')} »") %>
<% end -%>
Only makes it so that the pagination nav disappears but not the pagination as a whole.
It's a bit hackey but you could just set the per_page setting to something like 1,000 so it wouldn't ever have to paginate the results.
Throwing something like this in config/initializers/will_paginate.rb should work.
ActiveRecord::Base.instance_eval do
def per_page
1000
end
end

Resources