undefined method `total_pages' for ActiveRecord_Relation - ruby-on-rails

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.

Related

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>

Using search function in rails to retrieve a result and display it first in the index page

I am trying to implement search functionality in my rails app where I search and display a particular search result first on my index.html.erb view. At the moment I have a search function working and it returns the particular item on its own on the index page.
Ideally I would like to have this item displayed first and then all the other items to display below.
My code is as follows:
brand.rb
def self.search(query)
where("author like ?", "%#{query}%")
end
brand_controller.rb
def index
if params[:search]
#brand = Brand.search(params[:search]).order("created_at DESC")
else
#brand = Brand.all.order(':date')
end
end
I know the where method returns the value as an array so I could probably use array.first to output this result first but is there an easier way to output my desired view. Thanks!
So turned out to be a pretty simple solution, I blame mondays.
All I had to do was create another variable for my search and iterate that result first, then iterate through the rest of the items.
in my controller
def index
#brand = Brand.order('created_at DESC')
if params[:search]
#brand = Brand.search(params[:search]).order("author DESC")
#other = Brand.search_all(params[:search]).order("author DESC")
else
#brand = Brand.all.order('author DESC')
end
end
In my model
def self.search(query)
where("author like ?", "%#{query}%")
end
def self.search_all(query)
where("author not like ?", "%#{query}%")
end
and finally in my view
<% if #brand.any? %>
<% #brand.in_groups_of(2) do |group| %>
<% group.each do |brand| %>
<% if brand %>
<h4> <%= brand.author %></h4>
<a href="<%=brand_path(brand)%>">
<%=image_tag brand.brand_logo, class: 'img-rounded', :"data-uid" => brand.uid %> </a>
<% end %>
<% end %>
<% end %>
<% end %>
<% if #other %>
<% #other.in_groups_of(2) do |group| %>
<% group.each do |other| %>
<% if other %>
<h4> <%= other.author%></h4>
<a href="<%=brand_path(other)%>">
<%=image_tag other.brand_logo %> </a>
<% end %>
<% end %>
<% end %>
<% end %>

Running into undefined method `total_pages' with will_paginate

My goal is to have a tag cloud within my posts page which will allow the user to filter posts by clicking on a tag. However, I am running into an undefined method 'total_pages" error after making the following changes to my Post_Controller method:
class PostsController < ApplicationController
def index
if params[:tag]
#posts = Post.tagged_with(params[:tag])
else
#posts = Post.visible_to(current_user).where("posts.created_at > ?", 7.days.ago).paginate(page: params[:page], per_page: 10)
end
end
end
I am trying to use the acts-as-taggable-on gem, and this logic will show me the posts with the appropriate tags.
The issue happens in the posts/index.html.erb view:
<div class="row">
<div class="col-md-8">
<h1> Trending </h1>
<p class="lead"> Active posts this week </p>
<div id="tag_cloud">
Tag Cloud:
<% tag_cloud Post.tag_counts, %w[s m l] do |tag, css_class| %>
<%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>
</div>
<%= render partial: 'posts/post', collection: #posts %>
<%= will_paginate #posts %>
</div>
<div class="col-md-4">
</div>
</div>
The will_paginate line will not render all the posts on that page. The work around is getting rid of <%= will_paginate #posts %> and replacing
#posts = Post.visible_to(current_user).where("posts.created_at > ?", 7.days.ago).paginate(page: params[:page], per_page: 10)
with #posts = Post.all. However, this gives me an entire page of posts, which is ugly. Does anyone know why I am running into an undefined method 'total_pages' error?
It looks like when you're sending a tag (params[:tag]) it is fetching posts with
#posts = Post.tagged_with(params[:tag])
which is lacking the will paginate call. I believe you could get it working by adding the will paginate scope, like this:
#posts = Post.tagged_with(params[:tag]).paginate(...)

Will_Paginate pagination links missing

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!

Rails 4 - will_paginate

This is my first attempt using will_paginate (I know! Where have I been??)
titles_controller.erb
def index
#titles = Title.active.sorted.paginate(:page => params[:page])
end
index.html.erb
<% will_paginate #titles.each do |title| %>
Error:
undefined method `total_pages' for #<Enumerator:0x00000002bacaf0>
WTF am I doing wrong? Thanks in advance.
Please read will paginate docs. You need to write:
<%= will_paginate #posts %>
There is no need for adding each.
So entire view would look like:
<% #titles.each do |title| %>
<!-- do smth with title -->
<% end %>
<%= will_paginate #titles %>
In your case you dont acctually need to write:
<%= will_paginate #titles %>
Because it is in the context of the title_controller, will_paginate will assume their is a #titles variable available. Thus it is possible to just write:
<%= will_paginate %>

Resources