简体   繁体   中英

Rails 7 + Devise + Turbo stream not showing errors when sign in

I started new project and am stuck for hours trying to make Sign In page show errors. Though when I try to write incorrect data on Sign up page it returns errors, Sign in completely ignores it. I found decision concerning devise for rails 7 from gorails , it didn't help.

Gemfile:

gem "devise", git: "https://github.com/ghiculescu/devise.git", branch: "error-code-422"
gem "responders", git: "https://github.com/heartcombo/responders.git"

config/initializers/devise.rb:

Devise.setup do |config|
...
config.navigational_formats = ['*/*', :html, :turbo_stream]
...

Did it in order not to add new controller and custom error as it was shown in video. Also, console returns 422, but in terminal still:

Started POST "/users/sign_in" for ::1 at 2022-11-29 12:46:57 +0200
Processing by Devise::SessionsController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"test@example.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["email", "test@example.com"], ["LIMIT", 1]]
Completed 401 Unauthorized in 271ms (ActiveRecord: 0.3ms | Allocations: 1727)


Processing by Devise::SessionsController#new as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"test@example.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
  Rendering layout layouts/application.html.haml
  Rendering devise/sessions/new.html.haml within layouts/application
  Rendered devise/shared/_error_messages.html.haml (Duration: 0.0ms | Allocations: 12)
  Rendered devise/shared/_links.html.haml (Duration: 0.2ms | Allocations: 137)
  Rendered devise/sessions/new.html.haml within layouts/application (Duration: 3.5ms | Allocations: 1348)
  Rendered layout layouts/application.html.haml (Duration: 5.4ms | Allocations: 2506)
Completed 200 OK in 251ms (Views: 6.3ms | ActiveRecord: 0.0ms | Allocations: 3124)

My views are default generated by devise. If I should provide more info, like application.html.haml etc, please tell me.

This is a known issue with Devise, and for now the recommended fix is to disable Turbo on Devise's log in/out forms.

From https://github.com/heartcombo/devise/wiki/Troubleshooting-Rails-7-and-Turbo-Drive :

Since Turbolinks is replaced with Turbo Drive (That is part of Hotwire), we need to ensure full-page redirects when submitting devise forms.

One trick you can use for Devise's log out link, since it uses the HTTP DELETE method, is to change it to a button, like so:

<%= button_to "Log out", destroy_user_session_path, method: :delete, data: { turbo: false } %>

... but the wiki link above has other samples as well.

There are some more details and discussions here:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM