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)
Links to this issue
Commits related to this issue
- Rails 7에서 devise의 플래시 메시지가 표시되지 않는 버그 수정 https://github.com/heartcombo/devise/issues/5446 — committed to joyfuI/gagyebu by joyfuI 2 years ago
- Rails 7에서 devise의 플래시 메시지가 표시되지 않는 버그 수정 https://github.com/heartcombo/devise/issues/5446 — committed to joyfuI/gagyebu by joyfuI 2 years ago
- Added fix for devise issue 5446 https://github.com/heartcombo/devise/issues/5446 — committed to chanson02/CodeRacer by chanson02 2 years ago
- fix: Fixed devise config to work with Turbo Devise had issues with Turbo and did'nt work out of the box: https://github.com/heartcombo/devise/issues/5446 Workaround/solution as shown here: https://b... — committed to modulehandbook/modulehandbook by YoussefHenna 2 years ago
- Migration from Webpacker to Importmap (#108) * refactor: Migrated to importmap instead of webpacker Importmap (https://github.com/rails/importmap-rails) instead of the retired webpacker * Adde... — committed to modulehandbook/modulehandbook by YoussefHenna 2 years ago
- Integrate with Hotwire/Turbo by configuring error and response statuses Treat `:turbo_stream` request format as a navigational format, much like HTML, so Devise/responders can work properly. Allow c... — committed to heartcombo/devise by carlosantoniodasilva a year ago
- Integrate with Hotwire/Turbo by configuring error and response statuses Treat `:turbo_stream` request format as a navigational format, much like HTML, so Devise/responders can work properly. Allow c... — committed to heartcombo/devise by carlosantoniodasilva a year ago
- Integrate with Hotwire/Turbo by configuring error and response statuses Treat `:turbo_stream` request format as a navigational format, much like HTML, so Devise/responders can work properly. Allow c... — committed to heartcombo/devise by carlosantoniodasilva a year ago
- Integrate with Hotwire/Turbo by configuring error and response statuses Treat `:turbo_stream` request format as a navigational format, much like HTML, so Devise/responders can work properly. Allow c... — committed to heartcombo/devise by carlosantoniodasilva a year ago
- better sintax, using default devise method inside login_pass after 'wadern.auth do' to verify password and call sign_method, redirectin to root path with notice in the correct local of front end to co... — committed to JesusGautamah/outerspace-blockchain by JesusGautamah a year ago
+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:Turns out the issue is
javascript_importmap_tagsin application.html.erbRemove 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
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_formethod with thesedata: { 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:
This simply disables Turbo for this form submission (but not anywhere else).
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.
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.erbafter login failure. After addingturbo_streamto devise navigational_formats, sign_in request with turbo_stream format returns an:okstatus. However, flash messages will not be shown for login failures unless we explicitly set a failure status (eg :unprocessable_entity) Will show flash messageWill not show message
Any suggestions on how we can achieve a similar result with devise?
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.
@carlosantoniodasilva 🚀
@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.