If you’ve ever tried to have your Devise forms submit via AJAX, then you may have found that the resources to accomplish this are kind of spread out all over the internet.
Consider this my humble effort to bring all those bits and pieces of information together in a way that I wish was available when I took on this task. Without further ado, here are the steps I took to use Devise with Turbolinks.
Note: This assumes you already have a fairly default configuration of Devise.
First, we have to configure Devise to allow us to use jquery-ujs and SJR responses.
On to the views, if you have not already copied the devise views into your application, let’s go ahead and do that.
Now we’ll use jquery-ujs to easily make all the devise view forms submit via AJAX. Here’s an example.
Voila! Moving forward, we come to the SJR responses. Since our forms are now submitted via AJAX, we need to handle those responses properly. Here’s an example.
For most of the responses you’ll only have to deal with create/update, but when you try to write the response for your login, you’ll find an odd issue. It’s a small quirk that just means we’ll have to separate our logic from the previous example.
Finally, we’ll set up our flashes such that we get proper error messages from the responses. Let’s add that anywhere it makes sense in your layout.
And just like that we have Devise working in perfect harmony with Turbolinks!