Kaminari pagination scope problem - ruby-on-rails

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

Related

Rendering paginated pages in JBuilder views

I am currently paginating the return of a query attendees that has over 9000 items. My pages and routing work fine but I would like them to appear at the bottom of the page as clickable links to that page of the results. I am relatively new at using JBuilder I am using the Kaminari gem as well as the API-Pagination gem and would like to know how to I add visible/clickable page numbers to a JBuilder view according to Kaminari Docs <%= paginate #attendees %> is all that is needed. But as far as I understand JBuilder does not work or interpret that logic as its purely manufacturing JSON objects? Any advice is appreciated as well as a better explanation of what JBuilder is doing.
Controller
module Reports
class ConferencesController < ::ApplicationController
def attendees
#conference = Conference.find(attendee_params[:conference_id])
#attendees = #conference.attendees
paginate json: #attendees, per_page: 500
end
private
def attendee_params
params.permit(:conference_id)
end
end
end
View
json.conference #conference, partial: 'conference', as: :conference
json.attendees #attendees, partial: 'attendee', as: :attendee
<%= paginate #attendees %>
Kaminari works great of the box for HTML partials, but there are some additional things you need to do to set it up for other response formats. You can remove the paginate json: #attendees, per_page: 500 line from your controller in favor of something like
#attendees = #conference.attendees.page(params[:page]).per(500)
Additionally you will need to provide additional information to your jbuilder partial to render this information.
Something like this:
json.meta do
json.total_pages #attendees.total_pages
json.total_count #attendees.total_count
end
# See the Kaminari docs for more methods available https://github.com/kaminari/kaminari#the-page-scope

undefined method `paginate' despite having will_paginate gem

I'm a rails newbie and I'm trying to construct a feed of posts that are pulled from the database based on the characteristic of a category (in this case music). The action getting flagged is:
def music
#music_wads = Wad.find_by(category: "Music").paginate(page: params[:page])
end
Perhaps I'm making an error with the syntax, but I have the gem will_paginate installed. I have yet to actually add the <%= will_paginate #music_wads %> to my view however. Any thoughts?
Try .where instead of .find_by:
#music_wads = Wad.where(category: "Music").paginate(page: params[:page])
.find_by returns first record matching your conditions, while .paginate needs an ActiveRecord::Relation collection of records to work.

will_paginate with search function is not working in ruby on rails

I am trying to search my posts item with pagination. I am doing like this..
#posts = Post.search(params[:search]).paginate(page: params[:page],:per_page => 5)
But it showing NoMethodError
undefined method `paginate' for #<Array:0x9a93f08>
I don't know where i am wrong. Please help
I just change my code
#posts = Post.search(params[:search]).paginate(page: params[:page],:per_page => 5)
to this
#posts = Post.paginate(page: params[:page],:per_page => 5).search(params[:search])
Now it works fine :)
params[:search]) gives you an array. And Array don't have paginate method, so errors thrown up. Use p params[:search]).inspect to see the same.
I will advise you to use Kaminari instead. As you can see by the Readme there's one section on to paginate Arrays.
Also make sure you read the Kaminari recipes where is an example on paginate Arrays.

rails 3,Kaminari pagination for an simple Array

For paginating a common array I got this solution,
#arr_name =
Kaminari.paginate_array(#arr_name).page(params[:page]).per(PER_PAGE_RECORDS)
PER_PAGE_RECORDS is a variable with value as per needed for pagination.
Any better Ideas??
Also to have an ajax call for using pagination one can use this,
In your view,
give id to your div tab
div id="paginate"
and inside it
<%= paginate #arr_name, :remote => true %>
And in js response file put,
$('#paginate').html('<%= escape_javascript(paginate(#arr_name, :remote
=> true).to_s) %>');
So your requests will be AJAX.
Thanks.
This is the only available helper method to paginate an array object using Kaminari. Another alternative is, as suggested solution in kaminari wiki page, add the instance methods to the array object.
If you are trying a common solution based on the ActiveModel return type ( .all returns array and .where returns ARL) then following is an workaround.
unless #arr_name.kind_of?(Array)
#arr_name = #arr_name.page(params[:page]).per(PER_PAGE_RECORDS)
else
#arr_name = Kaminari.paginate_array(#arr_name).page(params[:page]).per(PER_PAGE_RECORDS)
end

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])

Resources