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.
Related
I have used Elastic Search to fetch the data and am trying to use Pagination gem to toggle between pages. But i am currently facing an issue in
<%= will_paginate #data %>
i am getting an error
NoMethodError - undefined method `total_pages' for #<Array:0x007fdcfbd3f128>
since #data is an array instead of an Active Record since the data was fetched from Elastic Search
Is there anyway to specify the total pages so that i will still be able to use the gem.
Your #data is an array instead of collection so to use will_paginate on array you can do like this:
require 'will_paginate/array'
And then:
#data.paginate(:page => 2, :per_page => 10)
will work.
Source: https://github.com/mislav/will_paginate/wiki/Backwards-incompatibility#willpaginatecollection
I am working on rails 4 and this are my steps:-
gem kaminari --in gemfile
bundle install
#vendors = Vendor.order(:name).page params[:page] in vendor_controller.rb)
also tried #vendors=Vendor.order("name").page(params[:page]).per(5)
<% = paginate #vendors %> in index.html.erb
Error:-
undefined method `page' for #<ActiveRecord::Relation::ActiveRecord_Relation_Vendor:0x16bec10>
I have also tried in console
Vendor.count
a = Vendor.limit(5).count
b = a.page(1).per(20).size
Error:-
NOmethoderror undefined method 'page'
question:-
Where i am going wrong .?
Thanks
Try calling the page method directly from the ActiveRecord class. You're calling it after order, which returns an ActiveRecord::Relation object.
#vendors = Vendor.page(params[:page]).order(:name)
I think this one may solve your problem:
vendor.except(:limit, :offset)
In model use:
paginates_per 5
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
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])
Excuse me I have a problem with will_paginate plugin.
In localhost, in my controller, I use person.operations.descend_by_date.paginate :page => params[:page] || 1. However in production the method show error. I think that the problem is thye parameter page. Because person.operation.descend_by_date return a collection i try passing only params[:page] || 1.
However in localhost show error. The rare subject is:
If I evaluate the method once => Show error
IF i evaluate the method twice or more times => works ok
Rails show this message error: hash parameters expected
Why could be the error?
First i convert the will_paginate object to array and then i use the method for arrays of will_paginate
person.operations.descend_by_date.to_a.paginate params[:page] || 1
Thanks