I am trying to return all records that match a search in Kaminari and paginate the results. However, I am getting the following error:
undefined method 'page'
my controller code:
#search = Sunspot.search(Building) do
fulltext params[:search]
end
#buildings = #search.results.page(params[:page]).per(15)
I think I am just not understanding how to use Kaminari?
page is a method you can call in relation, you can do this:
#buildings = Building.where(id: #search.results.map(&:id)).page(params[:page]).per(5)
Related
I'm trying to implement the will_paginage gem for my application right now. I'm getting an error when trying to add it to my controller here is what it's saying: undefined method "paginate" for #<Assignment::ActiveRecord_Associations_CollectionProxy:0x007f82f83548f0>
Has anybody seen this error before?
Here is my full code with error:
Controller:
def dashboard
#assignments = current_account.assignments.paginate(page: params[:page], per_page: 5)
#invitation = Invitation.new
render locals: { admin_policy: admin_policy }
end
So I'm trying to paginate a collection obviously... So why this error:
Error:
I am running the Kaminari gem for my pagination.
Controller
def dashboard
#projects = Project.find_by_user_id(current_user)
if #projects.size > 10
#projects.page(params[:page]).per(10)
end
end
Dashboard view
= paginate #projects, :theme => 'twitter-bootstrap-3', :remote => true
In my case, the #projects is sometimes only 1 record or even zero records. When it is nil, I get an error on the params[:page] being nil.
So this works
def dashboard
#projects = Project.page(params[:page]).per(10)
end
This gets error undefined method 'page' for #<Project:0x007f8cac5f14b0>
def dashboard
#projects = Project.find_by_user_id(current_user).page(params[:page]).per(10)
end
I think it is because the #projects is only a couple of records which is less than the 10 specified in .per
I tried adding a #projects.count or #projects.size but I get the error undefined method 'size' for #<Project:0x007f8c996865f0>
def dashboard
#projects = Project.find_by_user_id(current_user)
if #projects.size > 10
#projects.page(params[:page]).per(10)
end
end
What the hell am I doing wrong!? haha
I am guessing I can fix this in the first instance instead of trying to fix the second or third options. Any help would be greatly appreciated.
The issue is Project.find_by_user_id(current_user) returns an Array, not an ActiveRecord::Relation
You should do something like:
current_user.projects.page(params[:page]).per(10)
If your relationships are correctly setup.
Or:
Project.where(user_id: current_user.id).page(params[:page]).per(10)
The will_paginate gem isn't working after I changed a query to get followers/followed_users
How can I use will_paginate with this??
#users = #user.reverse_relationships.order("created_at DESC").collect { |r| User.find(r.follower) }
I've tried several options like:
#users = #user.reverse_relationships.order("created_at DESC").collect { |r| User.find(r.follower) }
#users = #users.paginate(:page => params[:page])
#users = #user.reverse_relationships.paginate(:page => params[:page]).order("created_at DESC").collect { |r| User.find(r.follower) }
Each time I get an error like undefined method "total_pages" or undefined method "paginate"
You should re-order your query so that you can call paginate and total_pages on an ActiveRecord::Relation instance, as will_paginate requires.
This would remove the collect which effectively turns your relation into an array.
This could be done with something like:
#relationships = #user.reverse_relationships.includes(:follower).order("created_at DESC")
And then just access the follower of each relationship in your view or whatnot.
This will also be more efficient - you won't be issuing a separate query for each follower, as your original code is doing.
I am trying to add Kaminari to my Rails app. I have included the gem and this is what my controller looks like:
def index
if params[:year]
if params[:year].size > 0
#songs = Song.where("year like ?", params[:year]).page(params[:page])
elsif params[:artist].size > 0
#songs = Song.where("artist_name like ?", params[:artist]).page(params[:page])
elsif params[:song].size > 0
#songs = Song.where("title like ?", params[:song]).page(params[:page])
end
else
#songs = Song.first(10).page(params[:page])
end
end
and then adding
<%= paginate #songs %>
in my view, the error I am getting is:
undefined method `page' for #<Array:0x007fab0455b4a8>
Not sure why this is coming up as I followed the docs step for step.
Kaminari uses paginate_array to paginate an array. 2 solutions:
First, you can use limit(10) instead of first(10):
#songs = Song.limit(10).page(params[:page])
Second, use paginate_array
#songs = Kaminari.paginate_array(Song.first(10)).page(params[:page])
I'd advise you rewrite your controller slightly. Better yet, move your filters to the model or a filter class. Look into present? for testing existence of params as that will check for nil and empty.
def index
#songs = Song
#songs = #songs.where("year like ?", params[:year]) if params[:year]
#songs = #songs.where("artist_name like ?", params[:artist]) if params[:artist]
#songs = #songs.where("title like ?", params[:song]) if params[:song]
#songs = #songs.limit(10).page(params[:page])
end
Tl;DR If you use Mongoid, use kaminari-mongoid instead of kaminari alone.
At Github it said, Kaminari supports Mongoid...so I went and installed gem 'kaminari' with the result: unknown method :page...later i found the mongoid adapter: kaminari-mongoid and that works now.
def search
#location = Location.find(params[:location_id])
start_date = DateTime.strptime(params[:start_date], "%m-%d-%Y")
end_date = DateTime.strptime(params[:end_date], "%m-%d-%Y")
#songs = #location.songs.find(:all, :conditions => {:play_date => start_date..end_date}).paginate(:page => params[:page], :per_page => 40)
render 'show'
end
Here is my error
undefined method `paginate' for #<Array:0x007fb00e49e6c8>
all works if i remove the will_paginate but i need it...any ideas or is there a better way to write this controller
Try writing
#songs = #location.songs.where(:play_date => start_date..end_date).paginate(:page => params[:page], :per_page => 40)
The difference? where returns an ActiveRelation object, while find retrieves all the matching objects in an array.
Hope this helps.
NoMethodError: undefined method `paginate' for []:Array
My will_paninate works perfectly but above error jumped out after upgrading to version 3.0.0.
Add following require will solve the issue:
require 'will_paginate/array'
Check out this post for the backward compatibility of will_paginate 3.0.
The will_paginate documentation states that combining .paginate with .find is not the way to go, because .find will load everything from your DB before .paginate has a chance to restrict the fetch.