Using "will_paginage" gem the correct way - Rails - ruby-on-rails

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:

Related

Kaminari Pagination TypeError

I`m using kaminari in rails 7 to make the apgination of my API, but when i try to see a page different of the first page, im taking a type error, when i try access "http://localhost:3000/products?page=3":
TypeError (no implicit conversion of Symbol into Integer)
My paginated products index:
# GET /products
def index
page_number = params[:page].try(:[], :number)
per_page = params[:page].try(:[], :size)
#products = Product.all.page(page_number).per(per_page)
paginate json: #products
end
Anyone has a clue to how solve this?
EDIT:
I created a initializer named api_pagination.rb and put on him:
ApiPagination.configure do |config|
config.page_param do |params|
params[:page][:number] if params[:page].is_a?(ActionController::Parameters)
end
config.per_page_param do |params|
params[:page][:size] if params[:page].is_a?(ActionController::Parameters)
end
end
And now i can access "http://localhost:3000/products?page%5Bnumber%5D=2&page%5Bsize%5D=12" , but it doesn't look right to me, or this is the right way?
Maybe you need to set default values? Try with:
page_number = params[:page].try(:[], :number) || 1
per_page = params[:page].try(:[], :size) || 20
if that doesn't work, check what's coming in those number and size params or share more information of your issue, like the whole error stacktrace, even better the full request log.

undefined method `+' for nil:NilClass - Values are not null

I am trying to create a method that loops through some objects and if a certain attribute is true, adds to the cost of the lesson to the money received, but keep getting the error undefined method `+' for nil:NilClass.
Here is the piece of code (the line producing the error start #activity.update_attribute):
def show
#activity = Activity.find(params[:id])
#participants = Participant.all.where(:activity_id => #activity.id).map(&:user_id).uniq
#all_participants = Participant.all.where(:activity_id => #activity.id)
#all_participants.each do |a_participant|
if a_participant.paid
#activity.update_attribute(:money_received, #activity.money_received + #activity.cost)
end
end
#users = Array.new
#participants.each do |participant|
#users.push(User.find(participant))
end
end
And here is a screenshot from my database to show that neither is a null value:
Here is the error message that I get when running the application:
Any help would be greatly appreciated
Many thanks

Kaminari error when collection is smaller than per(x)

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)

Will_Paginate gem error undefined method "total_pages"

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.

Kaminari and Sunspot undefined method page

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)

Resources