Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a form with method="get" submit with Turbolinks

I have a search form on my page, and it is safe for Turbolinks to be enabled as it is using method="get"

Is there any easy way to have the form submit under control of Turbolinks (ie avoid page refresh)

like image 333
Jak Charlton Avatar asked Jul 17 '13 02:07

Jak Charlton


2 Answers

You could use something like this in your application.js:

// send get forms through turbolinks
$(document).on("submit", "form[data-turboform]", function(e) {
    Turbolinks.visit(this.action+(this.action.indexOf('?') == -1 ? '?' : '&')+$(this).serialize());
    return false;
});

Then, to enable any form to be sent with turbolinks you would need to add the data-turboform attribute to the form, like this:

<form action="..." method="get" data-turboform>
    ...
</form>
like image 111
rabusmar Avatar answered Oct 06 '22 01:10

rabusmar


At the turbolinks project, there's issue #64 where someone has written a Coffeescript implementation for Rails.

Add the code provided at that link. It adds a turboforms function that needs to be called at page ready, like this:

$(turboforms);

I'm in the process of implementing this, I'll update my answer if I find out anything else that's useful.

like image 33
Robin Daugherty Avatar answered Oct 05 '22 23:10

Robin Daugherty