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/
Related
I have a method which fetches results using solr. for eg.
def test
#result1 = Model1.search do
fulltext params[:search]
paginate page: params[:page], per_page: 12
end
#result2 = Model2.search do
fulltext params[:search]
paginate page: params[:page], per_page: 12
end
end
Now I have 2 results and I am displaying this in test page across 2 tabs.
When I display content via tabs, the data gets populated correctly in each tab, but I have a problem in pagination.
When I click suppose say on page number 3 in result2 tab it displays page3 of result2 but when I click on result1 tab it takes me to page3 of result1 tab. Where as it should go to page1 when I click on the result1 tab.
<%= paginate #result1 ,:params => { :anchor => 'result1' }%>
<%= paginate #result2 ,:params => { :anchor => 'result2' }%>
How can I solve this. I am using kaminari gem for pagination.
You can use the option param_name in kaminari like so
<%= paginate #result1, param_name: 'result1' %>
and likewise for #result2. Then instead of params[:anchor] you should use params[:result1] to get the page number. Since you'll be using different param names for #result1 and #result2, they won't interfere with each other
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.
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.
Ok so I have decided to use Kaminari for pagination in a rails 3 project. I have followed the video from RailsCasts http://railscasts.com/episodes/254-pagination-with-kaminari
All goes well up until the point or running the server.
controllers/stories_controller.rb
def index
#stories = Story.all
#pages = Story.page(params[:page]).per(3)
#stories = Story.search(params[:search])
end
views/stories/index.html.erb
<%= paginate #pages %>
When i start the server the index page in question displays all the stories from the DB and renders the pagination view showing (1 2 Next > Last ยป). What am I missing to get the pagination working?
I still can not understand your code. Why do you assign Story.all to #stories in the 1st line and overwrite the variable in the 3rd line?
Anyways, #stories will display "all the stories from the DB" because you're not calling the pagination method (.per) on #stories. The pagination links will show you the paginated counts because you're calling per method on #page variable and passing it to the helper.
I mean, you need to call .per on the relation before passing it to <%= paginate %> helper.
It's quite simple.
I guess you want to get results from your search, right?
Try
#stories = Story.search(params[:search]).page(params[:page]).per(3)
and something like:
<% #stories.each do |story| %>
<%= render story %>
<% end %>
<%= paginate #stories %>
in your view
I'm using the will_paginate gem. The default is 30 elements per page. How do I customize this?
If your controller is called User, you can do something like this in your controller:
#users = User.paginate :page => params[:page], :per_page => 10, :order => 'name ASC' This will show 10 results per page.
In your view:
<%= will_paginate #users %>
See the per_page option here:
https://github.com/mislav/will_paginate/wiki
It will allow you to change the number displayed per page, for anytime that model is paginated.
For a controller/action specific approach see Raunak's answer.