I’m currently moving over an app from Turbolinks to Turbo.
I’m struggling to figure out the best way to migrate a common pattern I have in my current Turbolinks setup:
- I have infinite scroll set up with the Pagy gem and it grabs the next page via a
GET
request - I have many filter menus, which filter down results via a form using
method: :get
, responding withformat.js
- After the filter button is pressed, the paginated filtered results replace the current set, and scrolling down triggers the
js
response, which appends the new records, updates theinfinite-scroll
div, and triggers a few other DOM changes.
With the migration:
- In order to get the forms to work with Turbo, I’ve had to change them to submit via a
POST
request and respond withformat.turbo_stream
- The
turbo-stream
response updates the target div with the first page of results and an updatedinfinite-scroll
div - Now infinite scroll fails with 404s because a
GET
route no longer exists
Here’s what I need to happen:
- When I click to filter a list, the first page of results are returned and the
infinite-scroll
div is updated to load the second page - Scrolling down eventually triggers the second page to append to the first page, the current
infinite-scroll
div is then removed and updated or replaced with one for the third page.
What would be the idiomatic way to do this using Pagy and Hotwire?
Is there a way to trigger a Stimulus controller after my Rails controller has finished processing? If there is, I might be able to just move over the JS I already have that’s working well.
I’m hoping for high-level tips on which direction to take but if I’ve not given enough information or seeing particular parts of the code would be helpful, just let me know. Thank you very much!