devise: Devise 4.8.1 + Rails 7.0.0 | Undefined method 'user_url'
Pre-check
- Created a new rails-app with
rails new app-name -a propshaft -j importmap -c tailwind
- Added
gem 'devise', '~> 4.8', '>= 4.8.1'
toGemfile
and followed installation instructions
Environment
- Ruby 3.0.3p157
- Rails 7.0.0
- Devise 4.8.1
Current behavior
- Receiving
NoMethodError (undefined method 'user_url' for #<Devise::RegistrationsController:0x0000000000d638>)
upon successful user registration. - User gets persisted in database
Demo app to reproduce the error is available here
Expected behavior
- Redirect to
root_path
without error
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 38
- Comments: 25 (5 by maintainers)
Links to this issue
Commits related to this issue
- Add :turbo_stream as a default navigational format Fixes #5439 Rails 7 returns :turbo_stream as a request format out-of-the-box. — committed to nickrivadeneira/devise by nickrivadeneira 3 years ago
- Merge branch 'create-login' — committed to tokyodaruma/Rails-Friends-List by tokyodaruma 2 years ago
- Almost lost my sanity on this devise + rails7 bug My app worked fine until suddenly I got these errors: NoMethodError (undefined method 'user_url' for #<Devise::RegistrationsController:0x0000000000d6... — committed to BingeBounty/BingeBounty by ChillerDragon 2 years ago
- This app is broken I am fixing it This is a step from github issue 5439 It get error. Receiving NoMethodError (undefined method 'user_url' for #<Devise::RegistrationsController:0x0000000000d638>) up... — committed to jackparsons93/rails_7_devise_example by jackparsons93 2 years ago
- Fix a user registration bug. Add turbo_stream as navigational format in order to fix redirect after user registration. https://github.com/heartcombo/devise/issues/5439#issuecomment-997292547 — committed to marcel-strzalka/monte-cinema by marcel-strzalka 2 years ago
- Disable turbo on Devise forms See https://github.com/heartcombo/devise/issues/5439. — committed to design-history/design-history by tvararu 2 years ago
- Disable turbo on Devise forms See https://github.com/heartcombo/devise/issues/5439. — committed to design-history/design-history by tvararu 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
- I was having issue when submiting this form once the admin had passed the invitations limit.. I added data: { turbo: false } following the suggestion https://github.com/heartcombo/devise/issues/5439\... — committed to MartinSugasti/simplest-resumes by MartinSugasti 10 months ago
Ran into this at the same time as you đ
Quick fix, tl;dr
Add
:turbo_stream
as a navigational format. This line goes inconfig/initializers/devise.rb
. I havenât tested this extensively, but I think it should be fine.More detail
A few lines from a trace starting with the offending line in registrations controller: https://github.com/heartcombo/devise/blob/025b1c873491908b346e4d394f54481ec18fb02c/app/controllers/devise/registrations_controller.rb#L25
https://github.com/heartcombo/devise/blob/025b1c873491908b346e4d394f54481ec18fb02c/app/controllers/devise/registrations_controller.rb#L111-L113
https://github.com/heartcombo/devise/blob/025b1c873491908b346e4d394f54481ec18fb02c/lib/devise/controllers/helpers.rb#L264-L266
https://github.com/heartcombo/devise/blob/025b1c873491908b346e4d394f54481ec18fb02c/lib/devise.rb#L218-L220
Basically, requests in Rails 7 may come with format
:turbo_stream
rather than simply:html
. This causesis_navigational_format?
to be falsey and thusafter_sign_up_path_for
returnsnil
. Withlocation: nil
forresponds_with
, the redirect path resolves from the resource - in our case the user.Iâm happy to open a PR for this, though someone with more familiarity with turbo should sanity check it.
For now you have to disable turbo on devise links, i.e.
data: { turbo: false }
. I should have some time to look into more official turbo integration soon.@erik-brueggemann my understanding is that method: :delete wonât work with a link_to when not using UJS (which is not a dependency by default in Rails 7). So the sign_out method is going to be called as a GET, which is the fallback, resulting in an error.
You can try changing link_to to button_to (which I donât like), or add data-turbo-method=âdeleteâ, like so:
A problem I am trying to find a solution for with this approach is that upon singing out it wonât redirect to the root, but to the page the user was on before signing out, which, of course, most probably requires authorisation, so it is not the best experience.
The approach above works without the @nickrivadeneira fix (however, you might be experiencing a different issue). Iâm not sure what is it changes exactly in the behaviour. I was hoping it will be a fix for my issue, but it isnât⌠any help will be appreciated.
The changelog says that âPlease note that Turbo integration is not fully supported by Devise yet.â. It would be nice if someone familiar with the matter updates us on what does this mean exactly and if there is some ETA of when full support will be added. I donât really want to waste hours implementing hacky solutions which might make my app vulnerable. On the other hand, I donât want to be waiting forever, if devise wonât be fully updated to support Rails 7.
Hi @nortonandreev, the problem youâre describing is yet another one that resulted from the Rails 7 / turbo-rails aftermath as far as I can tell.
This particular issue here wasnât meant to address the log out mechanism, but rather user registration only. For that, the fix suggested by @nickrivadeneira makes sense to me - nevertheless youâve got a point! Itâs probably a good idea to open a separate issue to find a permanent solution for that?
But since weâve already brought it up, instead of changing from
link_to
tobutton_to
and instead of adding the:delete
data-tag, you might try editing yourconfig/devise.rb
to sign out viaGET
instead ofDELETE
Happy to open up another issue if yaâll think itâs two separate topics.
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.
just curious - wouldnât that open one up to csrf attacks?
As noted by @nortonandreev this worked for me in an app with Rails 7 and importmap
This solution worked for me too!
Hi everyone, This message is from Dec, 2022, I am currently making a rails app in which I do use devise, but certainly all of it works fine from registering a new user session to destroying the same. I am just a beginner at Ruby on Rails but this app needed nothing more.
This is what I did
I am attaching the repository You can check the application code to verify and letâs close the issue finally.
It works for me
<%= button_to 'Logout', destroy_user_session_path, method: :delete, form: {turbolink: false} %>
I added
, data: { turbo: false }
to alllink_to
andform_for
helpers on the Devise views.