Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails will_paginate on two objects on the same page does not work

I have seen something similar asked before, but I can not understand it, and I can not ask for help there because of low reputation to comment.

So, I am asking a new question. I am using will_paginate plug in for two objects on the same page, and they are working but both move simultaneously. For example, if I click page 2 on the first, page 2 changes even in the second pagination.

This is my code in the controller:

    @tasks = @student.tasks.paginate(page: params[:page], :per_page => 2)
    @plans = @student.plans.paginate(page: params[:page], :per_page => 2)         

And this is my code in the view:

    <%= will_paginate @tasks %>
    <%= will_paginate @plans %>

How can I make this work separately?

like image 581
uklp Avatar asked May 12 '15 00:05

uklp


1 Answers

Your controller is using the same parameter :page for each model. Your view then uses that very parameter to set the page for both models in the view. You can define a different parameter to work with each model. ie.

@tasks = @student.tasks.paginate(page: params[:tasks_page], :per_page => 2)
@plans = @student.plans.paginate(page: params[:plans_page], :per_page => 2)         

<%= will_paginate @tasks, :param_name => 'tasks_page'%>
<%= will_paginate @plans, :param_name => 'plans_page'%>
like image 83
OscillatingMonkey Avatar answered Oct 13 '22 01:10

OscillatingMonkey