Generic application controller action

I have a rails app that displays user activity on the RHS on each page.

Presently I pass the collection to the partial directly:

<%= render partial: "activities/activity", collection: current_user.activities.order(created_at: :desc) %>

I wish to now paginate this activity list.

current_user.activities.order(created_at: :desc).page(params[:page]).per_page(10)

I am guessing I need to have this set as an instance variable and have it placed in a route that can be accessed from the view.

My question is where should I define this instance variable as the route needs to be generic as the activity is displayed on a views.

If it helps I am doing the pagination with ajax, "remote: true".

1 answer

  • answered 2017-11-13 05:05 Toby 1 Kenobi

    I think what you need to do is have a controller action that receives the ajax request which would include the page number. This action would pull up the relevant activities for that page (using the code you put in the question) then it would render some js which would clear the activities panel and repopulate with the new page of actions. It would also have to re-render the pagination controls so that the links are updated with the new page numbers.

    If you're using a gem for pagination then it probably has a method to call to generate the pagination controls, and those controls will link to the route to the controller action that I described, passing the relevant page number as a parameter.