We've moved discussions to Discord

Render partial with collection and display counter for each object with Pagy Gem

tommersio
I have a collection where I want to display the index of each record.

For example:
Name, 1
Name, 2
Name, 3.
....

I use the built-in hidden index to accomplish this, but after adding pagination with pagy gem the index restarts every time I go to a new page. So if i display 20 records per page, the first index on page 2 should be 21, but it currently display 1.

teams_controller.rb
def index
if params[:query]
  @pagy, @teams = pagy(Team.includes(:stadium).global_search(params[:query]))
else
  @pagy, @teams = pagy(Team.order_by_score)
end
end

index.html.erb
<%= render partial: "teams/card", collection: @teams, as: 'team' %>

in my partial.html.erb
<%= team_counter +1   %>

----

Any idea on how I can display the index of each record without it having to restart on each page?

Anything that could help me would be greatly appreciated.

tommersio
I have a collection where I want to display the index of each record.

For example:
Name, 1
Name, 2
Name, 3.
....

I use the built-in hidden index to accomplish this, but after adding pagination with pagy gem the index restarts every time I go to a new page. So if i display 20 records per page, the first index on page 2 should be 21, but it currently display 1.

teams_controller.rb
def index
if params[:query]
  @pagy, @teams = pagy(Team.includes(:stadium).global_search(params[:query]))
else
  @pagy, @teams = pagy(Team.order_by_score)
end
end

index.html.erb
<%= render partial: "teams/card", collection: @teams, as: 'team' %>

in my partial.html.erb
<%= team_counter +1   %>

----

Any idea on how I can display the index of each record without it having to restart on each page?

Anything that could help me would be greatly appreciated.

Notifications
You’re not receiving notifications from this thread.
© 2022 Jumpstart Pro by GoRails, LLC