ActiveRecord Relation error - ruby-on-rails

def index
#users = User.order("name").page(params[:page]).per_page(5)
end
This is my UsersController where i try to view all users from database. Problem is because is see only this error:
undefined method `page' for #<ActiveRecord::Relation::ActiveRecord_Relation_User:0x000000044853d8>
Where is the problem?

You can do like this incase you have used will_paginate gem
#users = User.order("name ASC").paginate(:page => params[:page], :per_page => 5)
Take a look on the gem will_paginate

The usage of will_paginate is slight different than Kaminari. You should do:
#users = User.order('name').paginate(page: params[:page], per_page: 5)

Related

Adding Ransack Gem with Categories

I am displaying a list of gyms, and users can view gyms by province. I would like to add search capability using the Ransack gem, but I am not sure how to go about it with my current set up. Looking for some advice.
Gyms controller
def index
if params[:province].blank?
#gyms=Gym.all.order("created_at desc").paginate(:page => params[:page], :per_page => 18)
else
#province_id = Province.find_by(name: params[:province]).id
#gyms = Gym.where(province_id: #province_id).order("created_at DESC").paginate(:page => params[:page], :per_page => 18)
end
end
thank you in advance.
Figured it out. Just replaced my existing controller code with this:
#q = Gym.ransack(params[:q])
#gyms = #q.result.includes(:province).page(params[:page]).order("created_at DESC").paginate(:page => params[:page], :per_page => 18)

Name Error with will_paginate

I'm trying to use will_paginate and have done everything listed in the github installation/use instructions, but I'm getting a NameError when I try to load the page:
uninitialized constant BooksController::Books
In my Gemfile I have:
gem 'will_paginate', '~> 3.1.0'
In my controller I have:
def index
#paginate books, have 10 per page
#books = Books.paginate :page => params[:page], :per_page => 10
end
And in my index.html.erb, I have added:
<%= will_paginate #books %>
Has anyone encountered this? I've run bundle install, and the gem appeared to be installed.
Use like this #books = Book.paginate :page => params[:page], :per_page => 10
Model name should not be plurals

undefined method `paginate' for #<Class:0x0000010484b2c8>

I am using Rails 4. I am trying to use will_paginate and bootstrap-will_paginate to include pagination on the index portion of my Users controller. I installed the above gems.
The code that is complaining is here:
def index
#users = User.paginate(page: params[:page])
end
Is there any reason you see why this is failing?
try this code:
#users = User.all.paginate(page: params[:page])
it should work. Hope to be helpfull

Deprecation warning: #apply_finder_options

I get DEPRECATION WARNING: #apply_finder_options is deprecated. when trying this in my user.rb:
def User.search(search, page)
paginate page: page,
per_page: 10,
conditions: ['name LIKE ?', "%#{search}%"],
order: 'name'
end
Called through UsersController:
def index
#users = User.search(params[:search], params[:page])
end
The pagination is done with the will_paginate gem.
What is triggering the warning and how can I fix it? Been trying some googling, but I find the docs not too comprehensive!
I'm pretty sure you just need to pull the order and conditions options out of the paginate method and use Active Record for that instead:
def User.search(search, page)
order('name').where('name LIKE ?', "%#{search}%").paginate(page: page, per_page: 10)
end

Ruby on Rails: will_paginate is not working correctly

If I click the next or previous link it doesn't go to the next or previous page.
All posts are on same page, but there are links next, previous at bottom.
In PostsController:
#posts = Post.paginate(:per_page => 15, :page => params[:page], :order => 'created_at DESC')
in posts/index:
<%= will_paginate #posts%>
Where is the problem with will_paginate?
You have to order before paginate,
so, change it to
#posts = Post.order('created_at DESC').paginate(:per_page => 15, :page => params[:page])
Not sure if this is causing your error, but ordering should be done outside of will_paginate.
#posts = Post.paginate(:per_page => 15, :page => params[:page]).order('created_at DESC')
This is how it should be done in Rails 3.
I've also had trouble setting the per_page parameter within the controller. You could try setting it in the model instead.
class Post
self.per_page = 10
end
Updating to will_paginate , '3.1.7' solved my issues

Resources