Will_Paginate pagination links missing - ruby-on-rails

I have set-up will_paginate to paginate an array (list of customer orders) and am using the following code in my show view (the orders are nested on the seller's page);
<ol>
<% #seller.trans.paginate(:page => 1, :per_page => 6).each do |buy| %>
<li><%= buy.customer.name %>£<%= buy.sum %><%= buy.date %></li>
<% end %>
</ol>
This has placed the right restriction on the array (6 per page) and I can manually filter through by changing the page number to 1, 2 or 3 (I have 13 orders) but the 'Next/Previous' links are missing from the view.
What is it I'm doing incorrectly? Thanks.

i think the code should be something like this
<ol>
<% #buys = #seller.trans.paginate(:page => 1, :per_page => 6) %>
<% #buys.each do |buy| %>
<li><%= buy.customer.name %>£<%= buy.sum %><%= buy.date %></li>
<% end %>
</ol>
<%= will_paginate(#buys) %>
take a look at the man page of the will_paginate gem from here, https://github.com/mislav/will_paginate

Okey, so this is how I would go about it:
View:
<%= will_paginate #seller, renderer: BootstrapPagination::Rails %>
Controller:
#seller = *Model*.paginate(:page => params[:page], :per_page => 6);
This has been tested using the gem will_paginate-bootstrap
Good luck!

Related

undefined method `total_pages' for ActiveRecord_Relation

I have seen a couple of questions on this and tried them but it doesn't help. I am using will_paginate 3.0.7 gem. Where exactly am I going wrong?
Here is my code:
Categories_controller.rb:
def show
#category = Category.includes(:products).find(params[:categoryid])
#products = #category.products.paginate(page: params[:page], per_page: 10)
end
_product.html.erb
<%= will_paginate %>
<ul class="categories">
<% #products.each do |product| %>
<li>
<%= link_to product.productname,product %>
</li>
</ul>
Thank you
You're missing #products as an argument to will_paginate. Also, missed to end your loop. Try this:
<%= will_paginate #products %>
<ul class="categories">
<% #products.each do |product| %>
<li>
<%= link_to product.productname,product %>
</li>
<% end %>
</ul>
Hope this helps.
I had a very similar issue with the exact same type of naming and structure. Renaming the variable actually fixed it. So instead of:
#products = #category.products.paginate(page: params[:page], per_page: 10)
You may want to try this:
#category_products = #category.products.paginate(page: params[:page], per_page: 10)
Cf. https://stackoverflow.com/a/62977623/11293450.

will_paginate gem rendering each entry 4 times rails

I am using will_paginate with rails 4 and have the following code in my users controller:
def index
#users = User.paginate(page: params[:page], :per_page => 10)
end
and in my view:
<% provide(:title, 'All users') %>
<h1>All Users</h1>
<%= will_paginate %>
<ul class="users">
<% #users.each do |user| %>
<%= render #users %>
<% end %>
</ul>
<%= will_paginate %>
This renders all the users however there are 40 per page, not 10. Each page has the correct users but 4 times. For example it renders users 1 .. 10 then 1 .. 10 again and so on. There are currently 101 users and if I set the per page limit to 1 it renders 101 pages with one user on each page as it should however any limit > 1 and it breaks.
Any insight on how to fix it so only 10 users appear on each page would be much appreciated.
I found your mistake. You need to render #users once. Hope it works on your side. :)
<ul class="users">
<%= render #users %>
</ul>
also you need to render pagination with following code
<%= will_paginate #users %>
You need to reference the block variable user within the render call, not #users
<ul class="users">
<% #users.each do |user| %>
<%= render user %>
<% end %>
</ul>

How do you implement archive menu for blog system into rails3 app?

When you go to blog page, you will see the archive list on the menu.
In most cases, it shows something like this
'Archive'
2012(78)
-December(1)
-November(5)
-October(10)
...
2011(215)
2010(365)
I'm confident to make blog posting system by using scaffold.
But I have no idea how to make this Archive:(
Anyone come up with good idea to implement this to app easily???
Need your help!!
<h3>Archives </h3>
<% if #posts.to_a.empty? %>
<div class="post">
<p>No articles found...</p>
</div>
<% else %>
<% current_month = 0 %>
<% current_year = 0 %>
<% for article in #posts %>
<% if (article.created_at.year != current_year)
current_year = article.created_at.year
%>
<h3 class="archiveyear"><%= article.created_at.year%></h3>
<% end %>
<% if (article.created_at.month != current_month || article.created_at.year != current_year)
current_month = article.created_at.month
current_year = article.created_at.year
%>
<h4 class="archivemonth"><%= (Date::MONTHNAMES[article.created_at.month]) %></h4>
<% end %>
<div class="archivepost">
<%= link_to article.title, article_path(article), :remote => true %> on <%= article.created_at.strftime('%A')%> - <%= article.created_at.strftime('%d') + "th"%>
</div>
<% end -%>
<%end %>
This may help you. I ve not included the number of counts in this code. Actually m figuring how to do it. If u can let me know.
Also in the controller ive done this.
#posts = Article.order("created_at DESC")
The #posts is an array so the items inside it ll be ordered and then i can fetch the records according to it ordering.
Thanks.

kaminari and order_by

So Im listing out all the members of my site and grouping them by name so that the list will be organized better. So in my view all my members are grouped by the first letter of their member name like:
B
Bakedfish
Beercan Dan
Bigmike33x
C
Cynicalassassin
ect..
Anyway, I also want to paginate this list but I cant add Kaminari's pagination arguments to my controller if Im using order because I get an undefined method error.
so this doesnt work:
#members = Member.all.group_by{|u| u.fullname[0].titleize}.page(params[:page]).per(18)
my view looks like this:
<div class="content">
<%= paginate #members %>
</div>
<% #members.keys.sort.each do |starting_letter| %>
<h3>
<%= link_to starting_letter, {:action => :browse, :controller =>:members, :letter => starting_letter } %>
</h3>
<ol>
<% #members[starting_letter].each do |member| %>
<li>
<% if member.is_artist? %>
<%= link_to member.full_name, member_path(member), :class=>"artist" %>
<% else %>
<%= link_to member.full_name, member_path(member) %>
<% end %>
</li>
<% end %>
</ol>
<% end %>
Here is my error message:
NoMethodError (undefined method `page' for #<Hash:0x007f78d4bf48f8>):
app/controllers/members_controller.rb:10:in `index'
Kaminari adds page method to ActiveRecord::Relation but Member.all.group_by returns hash. That is why you get this exception.
I'd suggest to perform grouping after pagination, e.g.:
#members = Member.order(:full_name).page(params[:page]).per(18).to_a.group_by { |u| u.fullname[0].upcase }
UPDATE
In order to use paginate helper you could assign 2 variables, e.g.:
#paginated_members = Member.order(:full_name).page(params[:page]).per(18)
#members = #paginated_members.to_a.group_by { |u| u.fullname[0].upcase }
And pass #paginated_members to the paginate helper.

Rails 3 - Why is my Post#Show view showing all of its comments' database data in one big block after the comments list?

In a Rails 3 blog type of app, I have a polymorphic comments model, with Posts having Comments (through :commentable). The Post#Show page has a form for comments that post through AJAX to a comment list below. Everything is working fine EXCEPT that Rails is for some reason dumping a full list of the database entries for all comments on that post in []'s at the very bottom of my comments list. I can't figure out where this is coming from and why!
Here are maybe the relevant code chunks, please feel free to request more! Thanks in advance.
views/posts/show
...
<%= render 'comments' %>
...
views/posts/_comments
<%= render :partial => 'comments/form' %>
<ul class="comments">
<% if #post.comments.empty? %>
<li>none yet</li>
<% else %>
<%= render :partial => 'comments/comment' %>
<% end %>
</ul>
views/comments/comment
<%= #comments.each do |comment| %>
<li>
<p class="comment_body">"<%= comment.body %>"</p>
<br/>
<p class="comment_info"><%= comment.name %> - <%= time_ago_in_words(comment.created_at) %> ago </p>
</li>
<% end %>
posts controller
def show
#post = Post.find(params[:id])
#comments = #post.comments
#commentable = #post
#comment = Comment.new(:commentable => #post)
#title = #post.author
end
And here's where the error creeps in (from Page Source). Right after the last comment closes and before the comments closes:
....
</li>
[#<Comment id: 97, name: "hmmm?", body: "hmmm", created_a...
</ul>
What is that thing and why is it here!? Thanks.
it's the = in the #comments.each tag. It is returning the result of each, which is the whole array.
For example:
irb> [1,2].each {|i| puts i }
1
2
=> [1, 2]
So:
<%= #comments.each do |comment| %>
Should simply be:
<% #comments.each do |comment| %>

Resources