I used will_paginate gem for pagination. This is my code:
def index
#templates = Template.all
#templates = Template.paginate(:page => params[:offset], :per_page => params[:limit])
end
Template has 52 records in database. When this action is executed, it shows an invalid page error. I don't understand the problem. Is there anyone to help me?
Related
I'm trying to use a debugger in a homework assignment I'm working on. I installed the gem ruby-debug19, but I can't put it to use. Whenever I put the statement ruby-debug in a method, and I go to the console server it doesn't respond. It's like this:
http://imgur.com/zV1IPka
def index
ruby-debug
#search = params[:search] ? params[:search] : {}
#articles = Article.search_with_pagination(#search, {:page => params[:page], :per_page => this_blog.admin_display_elements})
if request.xhr?
render :partial => 'article_list', :locals => { :articles => #articles }
else
#article = Article.new(params[:article])
end
end
Any help would be really appreciated. Thanks.
If you want something flexible with the option of setting breakpoints and navigating through code I can recommend pry-debugger. If you want something with glitter and magic jazz_hands is the gem you are looking for.
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 using a gem, will_paginate, to paginate contents on a rails site.
It's usage is: (from the official example)
## perform a paginated query:
#posts = Post.paginate(:page => params[:page])
# or, use an explicit "per page" limit:
Post.paginate(:page => params[:page], :per_page => 30)
## render page links in the view:
<%= will_paginate #posts %>
Now in some views, I will need the pagination but some views I don't. So when using a common layout to render out the content, the pages without pagination will display an error.
will_paginate undefined method error
Is there a way to check if will_paginate is a valid function before it the view tries to display it?
Thanks in advance,
You could make use of rescue.
For example :
<%= do_something rescue do_something_else %>
You can use defined? will_paginate or will_paginate #posts rescue _do_something_else_.
The defined? tells you if the function is defined, any you can use it in an if, the rescue handles the exception.
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