Rails will_paginate on two objects on the same page does not work
Asked Answered
A

1

6

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?

Arlberg answered 12/5, 2015 at 0:0 Comment(0)
S
10

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'%>
Sheol answered 12/5, 2015 at 0:6 Comment(1)
Thank you for such clarity! Really helped me out tonight.Apraxia

© 2022 - 2024 — McMap. All rights reserved.