Rails 4 - will_paginate and mongodb - ruby-on-rails

I'm getting a no method error when trying to implement pagination on my rails project.
undefined method 'paginate' for #<Mongoid::Criteria:0x007fb256e54588>
In my controller I have:
#user = User.all.paginate(:page => params[:page], :per_page => 10)
In my index.html.erb file I have <%= will_paginate #user %> above my html <table> tag and below the closing html </table> tag
I have the will_paginate and mongoid-pagination gems installed properly. And I have tried restarting the dev server.. Am I missing anything else?

You want to use the will_paginate_mongoid gem:
It just creates a paginate method compatible with will_paginate interface [...]
The mongoid-pagination gem requires you to manually include Mongoid::Pagination in your models and doesn't specifically integrate with the will_paginate gem.

The will_paginate gem has support for Mongoid out of the box.
require 'will_paginate/mongoid'
In your controller action
User.all.paginate(page: params[:page], per_page: 10)

Related

How to fix Kaminari paginate error in view?

When I use Kaminari gem with the code:
Model:
def index
#posts = Post.page(params[:page])
end
And use it in index.html.erb:
It prints the next error:
undefined method `paginate' for #<#<Class:0x000000029bbd68>:0x000000029b3dc0>
red line on <%= paginate #posts %>.
How can I fix this problem? Is my #posts paginable? I think yes.
If it's in the Gemfile, then you just need to run bundle install instead of gem install Kaminari. I would then restart my rails server to ensure its initializers are run to properly require its files.

How to use pagination in rails 3.2.3?

I'm new in RoR, and I want to include pagination in my application.
Which is the best way to perform this task? Please suggest me. Is it possible to use will_paginate gem in rails 3.2.3 ?
If yes, in which method I should include:
Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')
Post.page(params[:page]).order('created_at DESC')
Yes, it is possible to use will_paginate gem in rails 3.2.3 applications.
And the code will depends on what each action will perform.
The code below usually goes on a #index method, in your PostsController.
#posts = Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')
So, in your posts/index.html.erb view file you can use the following code to display the pagination links:
<%= will_paginate #posts %>

Kaminari pagination scope problem

In Kaminari, this works:
<%= paginate #posts %>
But if I do something like:
<%= paginate #user.posts %>
I get
undefined method `current_page' for #<Class:0x58378e0>
How do I fix this? I heard inherited_resources helped, but I don't know how to use it to fix this bug.
Because in the first version #posts was probably the result of running something like
#posts = Post.page(2).per(50)
So Kaminari returned an array like object, which paginate helper method expects.
When you call #user.posts directly you are in fact just loading a plain old ActiveRecord association.
The short answer is that you need to let Kaminari generate the variable you are in fact using with the paginate helper method

Rails pagination of subset of a class

I have a rails app and I'm trying to set up pagination for a view of the Essays class. I'm new to rails... so I can do this for ALL of them, but I only want certain ones to be in this list (where all the essays are contained in Essay.find(Ranking.where(:user_id=>current_user.id).essay_id)).
home.html.erb contains (among other things):
`<%= will_paginate #essays%>
<ul class="users">
<%= render #essays %>
</ul>
<%= will_paginate #essays%>`
in the Pages Controller:
def home
#...
#essays = Essay.paginate(:page => params[:page])
end
I tried adding #essays=Essay.find(Ranking.where(:user_id=>current_user.id).essay_id) before the #essays=Essay.paginate(:page => params[:page]) but the method essay_id for the Ranking class is not available here. How do I get around this? Thanks!
This should work:
Essay.joins(:rankings)
.where(:rankings => {:user_id => current_user.id})
.paginate(:page => params[:page])
While this can be done with will_paginate. I've had some issues with this plugin for Rails 3. A much smoother solution, in my opinion, was to use another pagination plugin called Kaminari.
Assuming, essay_id is passed as a param, you could try:
#ranking = Ranking.where(:user_id => current_user.id, :essay_id => params[:essay_id]).page(params[:page]).per(10)
Or depending on your logic. If the essay object has already been identified in your controller:
#essay = Essay.find(1234)
#ranking = Ranking.where(:user_id => current_user.id, :essay_id => #essay.id).page(params[:page]).per(10)
And then, in your view:
<%= paginate #ranking %>
Even better, to get you started on Kaminari, you can view this rails cast. As noted from the rails cast:
The first-choice gem for pagination in
Rails is will_paginate, but the
currently released version doesn’t
support Rails 3. There is a
pre-release version available that
works but it hasn’t been updated for
several months. If will_paginate is no
longer in active development are there
any other gems we could use?
One alternative is Kaminari. This
seems to provide a cleaner
implementation of pagination and
offers several improved features, too,
so let’s try it in our application
instead.
Hope that helps!
Simply chain paginate method after find method:
#essays=Essay.find(Ranking.where(:user_id=>current_user.id).essay_id).paginate(:page => params[:page])

Rails 3 render :partials

I am migrating my 1.8.7 rails app to rails 3. But I have a problem with a partial:
I have the following partial:
in my cms controller :
#clients = Client.all
group = render_to_string :layout => 'layouts/window', :partial => 'clients/index'
in my "clients/index" partial:
<%= render :partial => 'clients/item', :collection => #clients %>
This worked great with rails 1.7.8 but with rails 3 only the partial in the index get's rendered!. So, to clarify this, the group variable in the controller doesn't get the html from the layout.
Also the weird thing is that the window layout is _window.erb (if I do window.html.erb or just window.erb rails can't find it which is strange).
Does anybody know if this behavior is normal for rails 3?
thanxs!
Partials in rails have to start with underscore. Try renaming your "item.html.erb" partial to "_item.html.erb".

Resources