A rails test is generating an error as follows
ActionView::Template::Error: undefined method `total_pages' for nil:NilClass
options[:total_pages] ||= scope.total_pages
for this specific line in the view: <%= paginate #carts %>
The controller action generates an empty array according to the following logic
if user?
#carts = Cart.order(created_at: :desc).where([user_id = ?, current_user.id]).page params[:page]
else
#carts = []
end
Kaminari is bundled
Using kaminari-core 1.2.2
Using kaminari-actionview 1.2.2
Using kaminari-activerecord 1.2.2
Using kaminari 1.2.2
a puts #carts.size does show 0 in the logging. so the array object exists but the scoping is not activating.
It appears the base settings of kaminari are not kicking in in this particular instance, whereas in another case #users = User.page params[:page] the test does not complain.
How does this get resolved?
paginate [] # undefined method `total_pages' for []:Array
paginate nil # undefined method `total_pages' for nil:NilClass
# ^ ^
# NOTE: from the error it looks like #carts is nil --'
kaminari needs an ActiveRecord class or relation object and page method has to be called on that class/object for it to become "paginatable" and for scoping to kick in:
paginate Cart # => undefined method `total_pages' for Cart:Class
paginate Cart.all # => undefined method `total_pages' for #<ActiveRecord::Rel...
paginate Cart.page(1) # => <nav class="pagination" ...
paginate Cart.all.page(1) # => <nav class="pagination" ...
Kaminari has a helper to paginate over an array:
#array = Kaminari.paginate_array([1,2,3]).page(1).per(10)
paginate #array # => <nav class="pagination" ...
An empty Cart relation object can also be used:
paginate Cart.none.page # => ""
https://github.com/kaminari/kaminari#paginating-a-generic-array-object
Related
I am trying to use pagy gem but I am getting no method error.
In my controller, I previously have
def index
#hires = [*current_user.student_hires.order('created_at desc')]
current_user.groups.includes(:group_hires).each do |group|
#hires.push(*group.group_hires.order('created_at desc'))
end
#hires = #hires.uniq(&:id)
end
but since I want to use Pagy, I changed it to
def index
#pagy, #hires = pagy([*current_user.student_hires.order('created_at desc')])
current_user.groups.includes(:group_hires).each do |group|
#hires.push(*group.group_hires.order('created_at desc'))
end
#hires = #hires.uniq(&:id)
end
and in my view, I have
<%== render partial: 'pagy/nav', locals: {pagy: #pagy} %>
But I am getting
undefined method `offset' for [#<Hire id: 12, grade: "Grade 1"
I am using pagy in another simpler controller and it works well but I can't get it to work on this controller index.
I was able to fix it using pagy_array
So I did
#pagy, #hires = pagy_array([*current_user.student_hires.order('created_at desc')])
and also added require 'pagy/extras/array' to the config/initializers
More instruction about it is here https://ddnexus.github.io/pagy/extras/array#gsc.tab=0
I'm trying to make a chart using Chartkick and am getting this error: undefined method 'group_by_day' for 10:Fixnum.
As recommended by this SO post, I installed the gem groupdate, so that isn't the problem.
My index method from my tasks_controller is:
def index
#tasks = Task.where(user_id: current_user.id).order("created_at DESC")
end
And my erb in tasks#index is:
<%= line_chart #tasks.map { |task|
{name: task.name, data: task.reps.group_by_day(:created_at)}
} %>
Can anyone see where I'm going wrong here?
I'm having trouble with will_paginate. It works perfectly with static pages but not in dynamic pages. In my controller I have:
def search
#prods = Prods.find_all_by_producer(params[:producer])
#items = #prods.paginate(:page => params[:page], :per_page => 10)
end
In my view:
<%= will_paginate #items %>
The first 10 items (the first page) are well displayed but when I try to navigate to next pages, I have:
undefined method `paginate' for nil:NilClass
Parameters : {"page"=>"2","locale"=>nil}
I understand the issue, there is no params[:producer] when it calls the second page so #prods returns nil. But how to do that, any ideas?
Add the #prods as param like this:
{:producer=>#producer} %>
Source: http://www.cowboycoded.com/2009/09/08/appending-parameters-on-a-will_paginate-link/
After headaches with ThinkingSphinx and Solr/Sunspot, we're trying out ElasticSearch and Tire as our search back-end - but I've hit a problem.
Here's my search command in the controller:
#results = Item.search params[:search], :page => ( params[:page] || 1 ), :per_page => 20
And this is the problem section of the view:
<%= page_entries_info #results %>
The error message I'm getting is
undefined method `offset' for #<Tire::Results::Collection:0xa3f01b0>
but only when there is more than one page's worth of results. If there are less than 20 items returned, then they get shown fine.
The only similar reported issue I could find elsewhere was solved by passing the :page and :per_page parameters into the search function, but I'm already doing that, to no avail.
Tire has a Pagination module but it doesn't define offset. You could file an issue with them to add it, but in the meantime you can monkeypatch it in your app:
Tire::Results::Pagination.module_eval do
def offset
(#options[:per_page] || #options[:size] || 10 ).to_i * (current_page - 1)
end
end
in my testapp, results are paginated just fine, with will_paginate 3.0 and tire 0.3. I wasn't aware will_paginate needed the offset method.
I've added it, however, copying over the "lint" test from will_paginate specs: https://github.com/karmi/tire/commit/e0e7730. Should be part of the next release.
I would like to paginate my objects with the Kaminari pagination gem. I have this line in my controller:
#products = Product.order("id").find_all_by_id(params[:id])
That line in my view:
<%= paginate #products %>
And that line in my model:
paginates_per 20
When I open my page where my objects are supposed to be listed, I have this error message :
undefined method `current_page' for #<Array:0x2964690>
The exception is raised at my <%= paginate #products %> line.
I have already made a pagination for another project and it was working really great. Could someone help me please ?
Thank you !
Edit:
The problem is that find_all_by_* returns an array, not an ActiveRecord::Relation.
You can do something like this instead
#products = Product.order("id").where("id IN (?)", params[:id])
Also, you should probably have a .page(params[:page]) in there.