Jamie Hill
Photo of Jamie Hill

UX Designer, CSS Wizard, Ruby on Rails Developer.

Handling GET remote forms with Turbolinks in RailsĀ 6

How to handle Ajax GET requests with Turbolinks, and render the response.

In response to this problem with Turbolinks.

import Turbolinks from 'turbolinks';

document.addEventListener('turbolinks:load', function(event) {
  for (let form of document.querySelectorAll('form[method=get][data-remote=true]')) {
    form.addEventListener('ajax:beforeSend', function (event) {
      const detail = event.detail,
            xhr = detail[0], options = detail[1];

      Turbolinks.visit(options.url);
      event.preventDefault();
    });
  }
});