Pagination links not showing on arrays - ruby-on-rails

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 %>

Related

Will Paginate + Rails with association

I'm having a problem where pagination works properly in displaying the correct number of pages to display all objects, but it's simply repeating all of the same objects on each page. (eg. If there are 10 objects and my per_page is 10, it'll show one page and all 10 items; if per_page is 5, it'll show 2 pages, but in both cases it keeps repeating ALL of objects from 1-10 on every page)
There is a HMBTM relationship, and all images associated in this case with the product model is in a join model.
I'm using this pagination gem: https://github.com/mislav/will_paginate/wiki
Products Controller
def show
#products = #product.images.all.page(params[:page]).per_page(10)
end
View
<% #product.images.each_slice(2) do |f,g| %>
...
<% end %>
<%= will_paginate #products %>
I already defined #products in the controller so I had to adjust my view so it didn't repeat the same code and just used the defined instance variable.
<% #products.each_slice(2) do |f,g| %>
...
<% end %>
<%= will_paginate #products %>

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 **

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.

Paginate Comments With will_paginate

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

adding search functionality to a paginated page in RoR

Here is the scenario:
I have a table which renders a list of groups (id,title,description,etc) like below which is paginated, it also has a search form:
<div id="navigation" style="background:white;border:1px outset cadetblue;">
<p>
<% form_tag groups_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</p>
</div>
<table>
.....
</table>
<%= paginate #groups %>
In my groups_controller.rb I have:
#groups = Group.search(params[:search])
#groups = Group.order("id").page(params[:page]).per(15)
which doesn't seem to be working. The pagination works but when I click
the search button it doesn't do anything.
The search function is implemented in the group.rb model:
def self.search(search)
if search
find(:all, :conditions => ['description LIKE ?', "%#{search}%"])
else
find(:all)
end
end
How do I solve this, how do I make my search form work and return results in a paginated way!
Here is the ultimate answer to this post. All the original poster needed to do was this:
#groups = Group.where("description LIKE '%#{params[:search]}%'").order('id').page(params[:page]).per(15)
In an offline discussion, it wasn't working. turns out he had BOTH will_paginate Gem and kaminari Gem for his pagination. He was crapping out when it came to .per(15). Deleting the will_paginate gem fixed it!
will_paginate isn't being actively upgraded, so if you have will_paginate and you are on Rails 3.X, get rid of will_paginate and use kaminari instead, it's a fairly painless swap out, just a bit of css work to format the pagination.
You need to combine those two calls into one, right now you are overriding the first setting of #groups with the second.
I'd add a scope to your group model
class Group
scope :search, lambda { |val| , find(:all, :conditions=>['description LIKE ?', "%#{val}%"]) }
end
Then you can do
#groups = Group.search(params[:search]).order("id").page(params[:page]).per(15)

Resources