Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turbo-Rails with Devise does not redirect Consistently Rails 6.1.3 Devise 4.7.3 Turbo Rails 0.5.9

THIS IS A COMPANION TO https://github.com/hotwired/turbo-rails/issues/122

REPRODUCTION APP CODE: https://github.com/jasonfb/TR001

Using Rails 6.1.3

Devise 4.7.3

Turbo Rails 0.5.9


Symptom 1:
-- using Turbo-Rails + Devise

When I click "Log In", the form submits but the page does not reload.

Step 1: go to

/users/sign_in

Enter a good username & password (you can sign up or make one on the command line)

enter image description here

Click "Sign In"

enter image description here

on the back end the request WAS processed and even redirected, but the redirect did not happen on the front end. You (the user) ARE actually logged in even though your page is hanging and did not redirect.

enter image description here

Result: Page hangs. Expected result: Turbo Rails redirect correctly


Symptom 2

When I click "Log Out", the page does nothing. on the backend I am actually logged out, but the page does not change. If I reload, I see that I am now logged out

Step 1: be logged in

Step 2: Click to "log out" enter image description here

Notice that the button stays 'grey' (clicked), the request is processed on the back end:

enter image description here

Result: Page hangs

Expected result: Page directs after the user is logged out

The user IS ACTUALLY logged out, but the page hangs and does not redirect.

like image 475
Jason FB Avatar asked Mar 13 '21 15:03

Jason FB


1 Answers

This has been fixed on the Devise main branch, but as of 2021-12-29 there is not a released version of Devise for Rails 7. If you want to use the latest main branch of Devise from Github, this problem should not affect you.

TEMPORARY WORKAROUND:

override the devise form (use rails generate devise:views generating your own custom views

then you will edit the registrations/new and sessions/new modifying both like so:

form_for(resource, as: resource_name, url: session_path(resource_name) ) do |f|

change it to

form_for(resource, as: resource_name, html: {'data-turbo' => "false"}, url: session_path(resource_name) ) do |f|

this tells Devise to fall back to non-Turbo interaction for the log-in and registration.

For your Logout links use Rails button_to instead of link_to and the symptom goes away. note: recommend as a temporary fix until the root cause is addressed

Again, button_to for LOGGING OUT must be like so:

, method: :delete, 'data-turbo': false

alternatively, a nested syntax like data: { turbo: false } should also work for either form_for or button_to

like image 130
Jason FB Avatar answered Sep 19 '22 11:09

Jason FB