Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect default route for registration#create #5463

Closed
duhlin opened this issue Jan 30, 2022 · 5 comments · Fixed by #5548
Closed

Incorrect default route for registration#create #5463

duhlin opened this issue Jan 30, 2022 · 5 comments · Fixed by #5548

Comments

@duhlin
Copy link

duhlin commented Jan 30, 2022

Environment

  • Ruby 3.0.3
  • Rails 7.0.1
  • Devise 4.8.1

Current behavior

With the default routing options, the url to get the form to sign_up is: resource/sign_up.
This returns a form which default action is to POST to resource.

In case of validation errors on this form, the url of the browser is then resource. If the user refreshes his browser, he get a 404 error.

Expected behavior

In case of validation errors, if the user refreshes, he should get the form from /resource/sign_up

Note:

The expected behavior is the correct one for the sign_in form. (Both get and post actions target /resource/sign_in)

This can be fixed for the sign_up form with:

module ActionDispatch::Routing
  class Mapper
    def devise_registration(mapping, controllers) #:nodoc:
      path_names = {
        new: mapping.path_names[:sign_up],
        edit: mapping.path_names[:edit],
        cancel: mapping.path_names[:cancel]
      }

      options = {
        only: [:edit, :update, :destroy],
        path: mapping.path_names[:registration],
        path_names: path_names,
        controller: controllers[:registrations]
      }

      # force :create to be at same url than :new
      resource :registration, options do
        get :new, path: mapping.path_names[:sign_up], as: 'new'
        post :create, path: mapping.path_names[:sign_up], as: 'create'
        get :cancel
      end
    end
  end
end
@evertonlopesc
Copy link

I had problems creating the devise views with users.
$ rails generate devise:views users

@mvpxtech
Copy link

You just need to edit the sign up button of the devise to this:

<%= f.submit "Sign up", data: {turbo: false} %>

@duhlin
Copy link
Author

duhlin commented Feb 25, 2022

Thanks @mvpxtech. I'm not sure to understand the relation with Turbo/Turbolinks here.

Unless I'm mistaken, when a form is submitted, the location of the browser changes to the url of the action of the form.

The default behavior of the registration controller called by the POST is either to:

  • render the form with the list of errors when there are some errors
  • or to redirect to another url when the registration is successful.

The consequence is that when there is an error, the location of the browser is set to /resource. Unfortunately, there is no 'GET' route by default for this path. A refresh of the page doesn't work as expected: 404 instead of the login form.

The same definition of the routes for sign_up than the ones for sign_in would fix the problem.

@mvpxtech
Copy link

@duhlin
There is a breaking change in Rails 7: Invalid form submissions have to return a 422 status code for Turbo Drive to replace the of the page and display the form errors. The alias for the 422 status code in Rails is :unprocessable_entity. That's why, since Ruby on Rails 7, the scaffold generator adds status: :unprocessable_entity to #create and #update actions when the resource couldn't be saved due to an invalid form submission.

carlosantoniodasilva added a commit that referenced this issue Jan 31, 2023
Treat `:turbo_stream` request format as a navigational format, much like
HTML, so Devise/responders can work properly.

Allow configuring the `error_status` and `redirect_status` using the
latest responders features, via a new custom Devise responder, so we can
customize the both responses to match Hotwire/Turbo behavior, for
example with `422 Unprocessable Entity` and `303 See Other`,
respectively. The defaults aren't changing in Devise itself (yet), so it
still responds on errors cases with `200 OK`, and redirects on non-GET
requests with `302 Found`, but new apps are generated with the new
statuses and existing apps can opt-in. Please note that these defaults
might change in a future release of Devise.

PRs/Issues references:

#5545
#5529
#5516
#5499
#5487
#5467
#5440
#5410
#5340

#5542
#5530
#5519
#5513
#5478
#5468
#5463
#5458
#5448
#5446
#5439
carlosantoniodasilva added a commit that referenced this issue Jan 31, 2023
Treat `:turbo_stream` request format as a navigational format, much like
HTML, so Devise/responders can work properly.

Allow configuring the `error_status` and `redirect_status` using the
latest responders features, via a new custom Devise responder, so we can
customize the both responses to match Hotwire/Turbo behavior, for
example with `422 Unprocessable Entity` and `303 See Other`,
respectively. The defaults aren't changing in Devise itself (yet), so it
still responds on errors cases with `200 OK`, and redirects on non-GET
requests with `302 Found`, but new apps are generated with the new
statuses and existing apps can opt-in. Please note that these defaults
might change in a future release of Devise.

PRs/Issues references:

#5545
#5529
#5516
#5499
#5487
#5467
#5440
#5410
#5340

#5542
#5530
#5519
#5513
#5478
#5468
#5463
#5458
#5448
#5446
#5439
carlosantoniodasilva added a commit that referenced this issue Jan 31, 2023
Treat `:turbo_stream` request format as a navigational format, much like
HTML, so Devise/responders can work properly.

Allow configuring the `error_status` and `redirect_status` using the
latest responders features, via a new custom Devise responder, so we can
customize the both responses to match Hotwire/Turbo behavior, for
example with `422 Unprocessable Entity` and `303 See Other`,
respectively. The defaults aren't changing in Devise itself (yet), so it
still responds on errors cases with `200 OK`, and redirects on non-GET
requests with `302 Found`, but new apps are generated with the new
statuses and existing apps can opt-in. Please note that these defaults
might change in a future release of Devise.

PRs/Issues references:

#5545
#5529
#5516
#5499
#5487
#5467
#5440
#5410
#5340

#5542
#5530
#5519
#5513
#5478
#5468
#5463
#5458
#5448
#5446
#5439
carlosantoniodasilva added a commit that referenced this issue Jan 31, 2023
Treat `:turbo_stream` request format as a navigational format, much like
HTML, so Devise/responders can work properly.

Allow configuring the `error_status` and `redirect_status` using the
latest responders features, via a new custom Devise responder, so we can
customize the both responses to match Hotwire/Turbo behavior, for
example with `422 Unprocessable Entity` and `303 See Other`,
respectively. The defaults aren't changing in Devise itself (yet), so it
still responds on errors cases with `200 OK`, and redirects on non-GET
requests with `302 Found`, but new apps are generated with the new
statuses and existing apps can opt-in. Please note that these defaults
might change in a future release of Devise.

PRs/Issues references:

#5545
#5529
#5516
#5499
#5487
#5467
#5440
#5410
#5340

#5542
#5530
#5519
#5513
#5478
#5468
#5463
#5458
#5448
#5446
#5439
@carlosantoniodasilva
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

4 participants