Getting Will_Pagination to work with Devise + acts_as_votable - ruby-on-rails

I'd like to enable signed-in users the ability vote on items and visit a page that lists all their votes in a paginated format. Currently clicking on the pagination links doesn't update the content, though will_paginate accurately knows how many links to render.
In other words if there are 15 voted on items, and will_paginate specifies outputting 5 items per page, I will see 3 will_paginate links. However clicking on the link doesn't update the content (I'm stuck on page 1). Any help would be much appreciated thanks.
In Controller
require 'will_paginate/array'
def myfaves
#user_likes = current_user.find_liked_items.paginate(:page => params[:page], :per_page => 5)
end
In View
<%= will_paginate #user_likes %>

I figured out the issue, in View when rendering items to the page I had to call
<% #user_likes.each do |songs| %>
This ensures
<%= will_paginate #user_likes %>
is tied to the items rendered on the page.

Related

How to use pagination in rails 4.2.5 and also have the ability to reorder the list with ajax?

I'm working on an forum-type app in Rails v 4.2.5. My index page is a list of all the questions being discussed in the application and they are default sorted by the created_at date. I am also using the Kaminari gem to paginate all of the questions (25 per page). I originally had my app set up like this:
Questions Controller:
def index
#questions = Question.order(:created_at).page params[:page]
end
Index View:
# I render a partial that iterates through the questions list to display
# the title of the questions, then I include the paginate code below.
<div class="pagination">
<%= paginate #questions %>
</div>
I eventually decided I wanted users to be able to sort the questions by different criteria (e.g., by total amount of upvotes, by total amount of responses for a question, and by recently asked questions). Right now, you can click a link corresponding to the type of sort you want and it will AJAX the new sorted list (a partial) onto the page. However, when I do this, the pagination does not work and when I click to see the second page of the results, everything becomes unsorted.
Index View with Sort Links:
<div class="sort_selection">
<h3> Sort By: </h3>
<%= link_to "By Upvotes", "/questions/top?sort=votes", class: "question_sort_link" %>
<%= link_to "Answers Provided", "/questions/top?sort=answers", class: "question_sort_link" %>
<%= link_to "Recently Asked", "/questions/top?sort=recent", class: "question_sort_link" %>
</div>
Index Controller:
def top
case params[:sort]
when "votes"
#questions = Question.sort_by_votes #sort_by_votes is a method in my Question model that performs a SQL query
when "answers"
#questions = Question.where.not(answers_count: nil).order(answers_count: :desc).limit(25)
when "recent"
#questions = Question.order(created_at: :desc).limit(25)
end
render partial: 'questions_list', layout: false
end
Javascript AJAX
$(document).on("click", ".question_sort_link", function(event){
event.preventDefault();
$.ajax({
method: "get",
url: $(this).attr("href")
}).done(function(sorted){
$('.questions_show_sorted').replaceWith(sorted);
});
});
I fooled around with the placement of the <%= paginate #questions %> in the view, as well as removed the 25 limit in my controller and added .page params[:page] after all of the queries in the Top route but I still cannot get the pagination to work after I've AJAX'ed a sorted list onto the page. Does anyone have any suggestions?
When you are switching pages the data about sorting is lost, because you are reloading site with different parameters. You can either try to pass this data (the column you are going to sort, and info is it asc or desc) to the new page, and sort it before loading, or paginate it using AJAX (but that means loading everything at the first load). I can't tell about the "pagination does not work problem", because I don't know what you mean.
In general, this thing you are trying to do is rather complicated, and there is no simple solution for that. There is a library for JS called Datatables that (in theory) makes it easier. There is another library called jQ-Bootgrid, and a ruby gem called "Smart listing".
I think you need to provide the pagination links in your ajax response and replace them in your javascript callback.
I assume that you return html rather than json, which will make this a bit awkward. Perhaps you could build up a json response with pagination links and html content
{
next: /list?page=3,
prev: /list?page=1,
content: "<ul>
<li>foo</li>
<li>bar</li>
</ul>"
}

will_paginate with 2 list on the same view

I have too list #comments and #eval in the same view. If I go to the page 2 in the #eval, the comments page go to the page 2 as well, there is a simple way to do this in the separated way?
You can define a custom parameter name (param_name) for each pagination link group and differentiate the page number by that:
# in view
<%= will_paginate #foos, :param_name => :foo_page %>
<%= will_paginate #bars, :param_name => :bar_page %>
# in controller
#foo = Foo.paginate(page: params[:foo_page])
#bar = Bar.paginate(page: params[:bar_page])
Find the list of possible options in the source code of the gem: https://github.com/mislav/will_paginate/blob/v3.0.5/lib/will_paginate/view_helpers.rb#L46

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.

Rails 3 & Kaminari pagination problem

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

Will_paginate Plugin on two objects on same page

Hello I am using will_paginte plugin on two objects on a same page. Like on stackoverflow. There is a profile page on which there is a pagination on two things
QUestions and answers.
I am having problem ie:--
when user is clicking on questions pagination page 2. answers page are also updating. The reason is both is sending a post variable ie
params[:page]
How to change this variable so that only one should be updated. and how to maintain that user should not lose the other page.
ie
he is on 3rd page of questions and 1st page of answers and now he click on 5th page of the questions the result should be 3rd page of questions and 5th page of answers.
You can specify a :param_name option to tell will_paginate the name of the parameter to use for the page number within URLs (the default is :page). So you could do:
<%= will_paginate #questions, :param_name => 'questions_page' %>
<%= will_paginate #answers, :param_name => 'answers_page' %>
will_paginate documentation

Resources