devise: Rails 7 devise flash messages not being shown

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"=>"asdf@asdf.com", "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", "asdf@asdf.com"], ["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

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 30
  • Comments: 33 (2 by maintainers)

Commits related to this issue

Most upvoted comments

+1

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

maybe it can help you.

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

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

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

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

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.

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.

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).

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

The latest stable version v4.9.2 should have all you need to, in case you don’t want to bundle from main directly.

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).

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?

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!

@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.

@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.