why is will_paginate throwing an expected 1 given 2 error? - ruby-on-rails

I have an issue where legacy code for pagination was implemented prior to me being hiring failed randomly today. I am running a heroku app with rails and using will_paginate for pagination. In my controller I have
`
def index
#publications = Publication.paginate(page: params[:page], per_page: 11).order('sort_date DESC')
end
`
in my view i have
`
<div class="container">
<%= will_paginate #publications %>
</div>
`
It throws the following error(which is leading to a 500 error in turn): wrong number of arguments(given 2, expected 1) for will_paginate.
change the .paginate function parameters to be(:page => params[:page], :per_page => 11) as mentioned in the will_paginate documentation it still throws the same error. if i delete the per_page option it also still throws the same given 2 expected 1 error. i've also updated will-paginate and still get the same error

Related

User-changeable pagination preferably using will_paginate gem

I am using the will_paginate gem and am trying to figure out how to make it so that the user browsing the site can change how many items he/she wants per page using a list, like the list on the right side of this picture
(random google image search).
I'm also new to rails so I would very much appreciate it if you could give me an idea as to where each code would go. Thanks for any help.
in Gemfile
gem 'will_paginate'
in your controller where you want to paginate
class SomeController < ApplicationController
def index
#somethings = Model.all.paginate(page: params[:page], per_page: params[:per_page])
end
end
in your view
<%= select_tag :per_page, options_for_select([5,10,20,50],params[:per_page].to_i),
:onchange => "if(this.value){window.location='?per_page='+this.value;}" %>
With will_paginate you would simply use the per_page option:
#posts = Post.paginate(
page: params[:page],
per_page: params[:per_page]
)
However you should be wary when taking user input and passing it to a SQL query like this. In this case it it prudent to cast the input to an integer:
irb(main):008:0> nil.to_i
=> 0
irb(main):009:0> "gobeligook".to_i
=> 0
per_page = params[:per_page].to_i.zero? ? params[:per_page].to_i : 30
#posts = Post.paginate(
page: params[:page],
per_page: per_page
)

How to make partial_counter work with pagination

I have a blogging application in which User has_many posts. I am using pagination with Booststrap. How can I make the partial_count method work with pagination? Currently, the count resets on every page instead of carrying over across pages.
posts_controller.rb
def index
#posts = Post.order("created_at desc").paginate(page: params[:page], :per_page => 12)
end
views/posts/index.html.erb
<%= render #posts %>
<%= will_paginate %>
views/posts/_post.html.erb
<%= post_counter +1%>
<%= post.name %>
The counter works fine on the first page. However, all subsequent pages also start with "1". How can I make subsequent pages start with (number of pages * 12 + 1) instead?
Thanks for your feedback!
Use #posts.offset to get the proper counter initialisation.

Will_paginate in Dynamic pages as "search"

I'm having trouble with will_paginate. It works perfectly with static pages but not in dynamic pages. In my controller I have:
def search
#prods = Prods.find_all_by_producer(params[:producer])
#items = #prods.paginate(:page => params[:page], :per_page => 10)
end
In my view:
<%= will_paginate #items %>
The first 10 items (the first page) are well displayed but when I try to navigate to next pages, I have:
undefined method `paginate' for nil:NilClass
Parameters : {"page"=>"2","locale"=>nil}
I understand the issue, there is no params[:producer] when it calls the second page so #prods returns nil. But how to do that, any ideas?
Add the #prods as param like this:
{:producer=>#producer} %>
Source: http://www.cowboycoded.com/2009/09/08/appending-parameters-on-a-will_paginate-link/

Tire search and will_paginate - undefined method `offset'

After headaches with ThinkingSphinx and Solr/Sunspot, we're trying out ElasticSearch and Tire as our search back-end - but I've hit a problem.
Here's my search command in the controller:
#results = Item.search params[:search], :page => ( params[:page] || 1 ), :per_page => 20
And this is the problem section of the view:
<%= page_entries_info #results %>
The error message I'm getting is
undefined method `offset' for #<Tire::Results::Collection:0xa3f01b0>
but only when there is more than one page's worth of results. If there are less than 20 items returned, then they get shown fine.
The only similar reported issue I could find elsewhere was solved by passing the :page and :per_page parameters into the search function, but I'm already doing that, to no avail.
Tire has a Pagination module but it doesn't define offset. You could file an issue with them to add it, but in the meantime you can monkeypatch it in your app:
Tire::Results::Pagination.module_eval do
def offset
(#options[:per_page] || #options[:size] || 10 ).to_i * (current_page - 1)
end
end
in my testapp, results are paginated just fine, with will_paginate 3.0 and tire 0.3. I wasn't aware will_paginate needed the offset method.
I've added it, however, copying over the "lint" test from will_paginate specs: https://github.com/karmi/tire/commit/e0e7730. Should be part of the next release.

Error with Kaminari pagination

I would like to paginate my objects with the Kaminari pagination gem. I have this line in my controller:
#products = Product.order("id").find_all_by_id(params[:id])
That line in my view:
<%= paginate #products %>
And that line in my model:
paginates_per 20
When I open my page where my objects are supposed to be listed, I have this error message :
undefined method `current_page' for #<Array:0x2964690>
The exception is raised at my <%= paginate #products %> line.
I have already made a pagination for another project and it was working really great. Could someone help me please ?
Thank you !
Edit:
The problem is that find_all_by_* returns an array, not an ActiveRecord::Relation.
You can do something like this instead
#products = Product.order("id").where("id IN (?)", params[:id])
Also, you should probably have a .page(params[:page]) in there.

Resources