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
Related
I am using kaminari library, and I want to display users' pictures.
In my UserController, I put
def index
#users = User.order(:name).page params[:page]
end
Before, I was accessing the pictures like
#users.transitions-enabled
- #users.each do |user|
- user.pictures.each do |pic|
.box.panel.panel-default
= link_to (image_tag(pic.image.url(:origin))), user
I am now wondering how I can display images by using the following command:
<%= paginate #users %>
Edit:
Now I changed the code in UserController to
#users = User.all
#pictures = #users.pictures.page(params[:page]).per(2)
And it is giving me the error
undefined method `total_pages' for nil:NilClass
I was following the answer here but I am not sure how I can apply it in my case.
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
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)
I am trying to apply pagination to my rails app using Kaminari. I am also incorporating a simple search form based on the Railscast Episode #37. When I try to apply the kaminari page and per methods I get the error 'undefined method page'. Below is the code I'm using.
posts_controller.rb
def index
#posts = Post.search(params[:search]).page(params[:page]).per(2)
end
post.rb
def self.search(search)
if search
find(:all, conditions: ['title || body LIKE ?', "%#{search}%"], order: "created_at DESC")
else
find(:all)
end
end
index.html.erb
<%= paginate #posts %>
When I remove the pagination the search works fine. When I remove the search the pagination works fine. I just can't seem to use them both and have the code function properly. Please advise if there is something in my code that I am missing that is causing this not to work properly.
In your case, you are returning array object from the search method not ActiveRecord::Relation object.
find(:all, conditions: ...) # find method will return an array object.
Add check in your controller,
def index
#posts = Post.search(params[:search])
if #posts.class == Array
#posts = Kaminari.paginate_array(#posts).page(params[:page]).per(10)
else
#posts = #posts.page(params[:page]).per(10) # if #posts is AR::Relation object
end
end
Kaminari pagination with an array https://github.com/amatsuda/kaminari#paginating-a-generic-array-object
for ActiveRecord::Relation object, checkout this http://railscasts.com/episodes/239-activerecord-relation-walkthrough
When ever a perform a search query i get wrong number of arguments 0 of 1
my controller code is as follows
def search
#search = User.search do
fulltext params(:search)
end
#friends = #search.results
end
and my model is as follows
searchable do
text :name
end
pls what am i getting wrong here or the gem does not support rails 3.1. thanks
Your controller should be like this:
def search
#search = User.search do
fulltext params(:search)
end
#friends = #search.results
end
The params variable is a hash and should be accessed as such, using [] not ().