devise icon indicating copy to clipboard operation
devise copied to clipboard

Rails 7 devise flash messages not being shown

Open steveroot opened this issue 4 years ago • 21 comments

Environment

  • Ruby 3.0.2p107
  • Rails 7.0.0
  • Devise 4.8.1

Current behavior

A new rails 7 app with devise. Flash messages from Devise are not shown but Flash messages generated from another controller are shown. The Devise flash messages appear to be being generated in the console rendering messages when a new user is registered with a password that is too short


Started POST "/users" for ::1 at 2021-12-24 08:02:10 +0000
Processing by Devise::RegistrationsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  TRANSACTION (0.1ms)  begin transaction
  User Exists? (2.7ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "[email protected]"], ["LIMIT", 1]]
  TRANSACTION (0.2ms)  rollback transaction
  Rendering layout layouts/application.html.erb
  Rendering /home/sroot/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/devise-4.8.1/app/views/devise/registrations/new.html.erb within layouts/application
  Rendered /home/sroot/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/devise-4.8.1/app/views/devise/shared/_error_messages.html.erb (Duration: 0.7ms | Allocations: 582)
  Rendered /home/sroot/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/devise-4.8.1/app/views/devise/shared/_links.html.erb (Duration: 0.2ms | Allocations: 92)
  Rendered /home/sroot/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/devise-4.8.1/app/views/devise/registrations/new.html.erb within layouts/application (Duration: 3.9ms | Allocations: 2199)
  Rendered layout layouts/application.html.erb (Duration: 77.5ms | Allocations: 4429)
Completed 200 OK in 473ms (Views: 78.7ms | ActiveRecord: 3.1ms | Allocations: 15288)

Example minimal app: https://github.com/steveroot/rails7deviseflash (Add a 'new club' to see working flash message, Register a new user with a short password to not see flash message yet see render message in console)

Expected behavior

Flash messages shown on screen when generated

steveroot avatar Dec 30 '21 14:12 steveroot

Update, I found an error reported in the browser console, i don't know if this is useful

turbo.es2017-esm.js:2115 Error: Form responses must redirect to another location

A failure in the clubs controller generates a flash error message and a different message in the console turbo.es2017-esm.js:430 POST http://localhost:3000/clubs 422 (Unprocessable Entity)

image

The devise part generates the 'must redirect' message in the console like this screenshot

image

steveroot avatar Dec 30 '21 15:12 steveroot

I am also having this issue.

I thought that this might fix it but it doesn't...

ref: https://github.com/heartcombo/devise/issues/5439 Ran into this at the same time as you 😆

Quick fix, tl;dr

Add :turbo_stream as a navigational format. This line goes in config/initializers/devise.rb. I haven't tested this extensively, but I think it should be fine.

config.navigational_formats = ['*/*', :html, :turbo_stream]

There's also this open PR that may be of use: https://github.com/heartcombo/devise/pull/5440

AlanDonohoe avatar Dec 30 '21 17:12 AlanDonohoe

I am also having this issue.

I thought that this might fix it but it doesn't...

ref: #5439 Ran into this at the same time as you 😆

Quick fix, tl;dr

Add :turbo_stream as a navigational format. This line goes in config/initializers/devise.rb. I haven't tested this extensively, but I think it should be fine.

config.navigational_formats = ['*/*', :html, :turbo_stream]

There's also this open PR that may be of use: #5440

Thanks, it's nice to know it's not just me having trouble :-) I just tried it too and you're right that didn't work either, same error messages in the browser console too. Worth a try though, thanks again

steveroot avatar Dec 30 '21 17:12 steveroot

I am also having this issue. I thought that this might fix it but it doesn't... ref: #5439 Ran into this at the same time as you laughing

Quick fix, tl;dr

Add :turbo_stream as a navigational format. This line goes in config/initializers/devise.rb. I haven't tested this extensively, but I think it should be fine.

config.navigational_formats = ['*/*', :html, :turbo_stream]

There's also this open PR that may be of use: #5440

Thanks, it's nice to know it's not just me having trouble :-) I just tried it too and you're right that didn't work either, same error messages in the browser console too. Worth a try though, thanks again

Did it work for you, I just tried it, it's not working for me.

Note: after 2 hours i found this episode of gorails, it's solved https://www.youtube.com/watch?v=yZDTBItc3ZM&t=316s&ab_channel=GoRails

bouaik avatar Jan 01 '22 11:01 bouaik

@bouaik Nice one! Thank you for sharing that.

Bit of a fiddly one, but it's mostly working for me. Other than a failed log in attempt (logs below) - this just returns to the log in page without any error message.

Have you got this working too?

logs:

Started POST "/users/sign_in" for ::1 at 2022-01-02 11:57:55 +0000
Processing by Devise::SessionsController#create as TURBO_STREAM
...
Completed 401 Unauthorized in 7ms (ActiveRecord: 0.3ms | Allocations: 2129)

Started GET "/users/sign_in.turbo_stream" for ::1 at 2022-01-02 11:57:55 +0000
Processing by Devise::SessionsController#new as TURBO_STREAM

No flash message being set...

AlanDonohoe avatar Jan 02 '22 12:01 AlanDonohoe

@AlanDonohoe , yes it's working for me, setting :turbo_stream as a navigational format solves the 401, but not the flash messages, you have to add the same code as in the video to make it all working.

bouaik avatar Jan 02 '22 18:01 bouaik

And full code here https://github.com/hotwired/turbo/issues/45 from the very clever @excid3 - thank you so much! 👏

AlanDonohoe avatar Jan 03 '22 16:01 AlanDonohoe

I am an absolute beginner, so pardon me for any mistake. But I have been facing a similar issue to this, where I cannot sign up a user. The sqlite3 database remains empty even after pressing sign up.

The browser console shows the following error: Error: Form responses must redirect to another location.

While terminal serves me a rollback transaction:

Started POST "/users" for ::1 at 2022-01-04 16:41:34 +0530
Processing by Devise::RegistrationsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  TRANSACTION (0.1ms)  begin transaction
  User Exists? (0.2ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "[email protected]"], ["LIMIT", 1]]    
  TRANSACTION (0.1ms)  rollback transaction

I tried pasting the following in config/initializers/devise.rb but have had no success. Kindly help me implement user authentication. config.navigational_formats = ['*/*', :html, :turbo_stream]

Demo Github Repo : https://github.com/jaindivij21/friends-list

jaindivij21 avatar Jan 04 '22 11:01 jaindivij21

maybe it can help you.

nejdetkadir avatar Jan 06 '22 15:01 nejdetkadir

A temporary solution/hack would be to set: data: { turbo: false }, as in:

<%= form_with url: some_path, data: { turbo: false } do |f| %>
   ...
<% end %>

john-999 avatar Mar 30 '22 18:03 john-999

+1

Surprised Rails 7 isn't working out of the box with Devise yet 🤔

DaniG2k avatar May 07 '22 06:05 DaniG2k

A temporary solution/hack would be to set: data: { turbo: false }, as in:

<%= form_with url: some_path, data: { turbo: false } do |f| %>
   ...
<% end %>

Hi guys, that's works for me!

caiohenrique12 avatar Jun 03 '22 01:06 caiohenrique12

Turns out the issue is javascript_importmap_tags in application.html.erb

image

Remove it and Devise will start showing flash messages. I am not sure why this happens, but it happens. If someone can understand the reason, do explain it. Thanks

awais-amjed avatar Jun 13 '22 18:06 awais-amjed

@awais-amjed You are right. I don't know why it works after I removed it. Thanks a lot

bushmusi avatar Jul 04 '22 11:07 bushmusi

@awais-amjed Thanks, that worked for me as well

ohashioji avatar Jul 19 '22 05:07 ohashioji

Can you actually comment that out without breaking all of the hotwire stuff like turbo and stimulus?

amo13 avatar Jul 19 '22 06:07 amo13

@amo13 No, actually that's not a good way to do this. We should use above solutions data: { turbo: false } I figured out later when I needed to use hotwire that removing this line breaks all that stuff.

SO GUYS DON'T REMOVE THAT LINE IF YOU NEED HOTWIRE.

awais-amjed avatar Jul 19 '22 07:07 awais-amjed

Is this fixed now, I don't really want to be putting in hacks to get things working ?

lperry65 avatar Jul 21 '22 17:07 lperry65

I think the issue is about setting the right response status when devise renders new.html.erb after login failure. After adding turbo_stream to devise navigational_formats, sign_in request with turbo_stream format returns an :ok status. However, flash messages will not be shown for login failures unless we explicitly set a failure status (eg :unprocessable_entity) Will show flash message

def create user = User.find_by_email(sessions_params[:email]) if user&.authenticate(sessions_params[:password]) # do some stuffs flash[:notice] = 'Welcome' redirect_to redirect_location else flash.now[:alert] = 'Invalid Email/Password' render :new, status: :unprocessable_entity end end

Will not show message

def create user = User.find_by_email(sessions_params[:email]) if user&.authenticate(sessions_params[:password]) # do some stuffs flash[:notice] = 'Welcome' redirect_to redirect_location else flash.now[:alert] = 'Invalid Email/Password' render :new end end

Any suggestions on how we can achieve a similar result with devise?

wandji20 avatar Aug 04 '22 01:08 wandji20

maybe it can help you.

That also worked for me. And like @DaniG2k I'm also wondering why this is not working out of the box.

d33pjs avatar Aug 08 '22 19:08 d33pjs

I tried both approaches and they worked just fine. Finally decided to override form_for method with these data: { turbo: false } options.

📎 I created a gist with all the information may come in handy for this issue.

bellatrix988 avatar Aug 29 '22 15:08 bellatrix988

Is this issue still relevant? What is the current status of this problem?

marcel-strzalka avatar Oct 04 '22 08:10 marcel-strzalka

This issue is still unresolved. Use this working hack for now:

A temporary solution/hack would be to set: data: { turbo: false }, as in:

<%= form_with url: some_path, data: { turbo: false } do |f| %>
   ...
<% end %>

This simply disables Turbo for this form submission (but not anywhere else).

john-999 avatar Oct 04 '22 09:10 john-999

A temporary solution/hack would be to set: data: { turbo: false }, as in:

<%= form_with url: some_path, data: { turbo: false } do |f| %>
   ...
<% end %>

This is the correct answer.

dan avatar Nov 23 '22 23:11 dan

data: { turbo: false }

Thank you so much! This saved me.

rex-9 avatar Dec 05 '22 11:12 rex-9

<%= bootstrap_form_with model: @company, data: { turbo: false } do |form| %> it worked

KapilDevPal avatar Jan 30 '23 12:01 KapilDevPal

@carlosantoniodasilva :rocket:

Gubio avatar Jan 31 '23 22:01 Gubio

The main branch should contain all that's necessary for fully working with Turbo now, which should fix this. A new version will be released soon, but feel free to test it out from the main branch in the meantime, and report back on any issues. Thanks.

carlosantoniodasilva avatar Feb 09 '23 21:02 carlosantoniodasilva

The main branch should contain all that's necessary for fully working with Turbo now, which should fix this. A new version will be released soon, but feel free to test it out from the main branch in the meantime, and report back on any issues. Thanks.

Thank you for fixing and sharing, and my apologies to all here that since I discovered this and created the issue a year ago I've had zero time to code and respond (I code for fun alone and this year was too busy for me, that time will come again and I appreciate all of your help).

steveroot avatar Feb 11 '23 14:02 steveroot

@awais-amjed thank, I remove <%= javascript_importmap_tags %> and it work for me.

yayahc avatar Apr 09 '23 17:04 yayahc