Kevin Urbaum

Joined

10 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Infinite Scroll in Rails with Stimulus.js Discussion

So for everyone on Rails7, the Rails.ajax method does not work anymore. At least for me this was the case. Therefore, you can simply switch the infinite_scroll_controller to a fetch method:

loadMore() {
let next_page = this.paginationTarget.querySelector("a[rel='next']")
if (next_page == null) { return }
let url = next_page.href
fetch(url, {
headers: { "Accept": "application/json" }
})
.then(response => response.json())
.then((data) => {
this.entriesTarget.insertAdjacentHTML('beforeend', data.entries)
this.paginationTarget.innerHTML = data.pagination
})
}

This works on mobile without any problems as well.