Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

Devise redirects to /users when there is an error editing profile

Gems / Libraries • Asked by Alan Reid

I would like to know how i can change the redirect path so that when my editing a user fails it redirects back to users/edit and not to /users.

To replicate this i am entering a duplicate email address, entering the password. When my app checks the email it comes back as failed. You will see the page is localhost:3000/users

Any help or advise would be much appreciated thanks :D


I would actually recommend not trying to modify this because that's how Rails generally works. The thing is that your form submits a PUT request to /users when you edit your user, so when it fails, the request returns HTML and that's why your browser ends up on /users instead of /users/edit. Since it doesn't do a redirect in order to preserve all the variables, it has to keep the URL on /users in order to generate the next page that includes the errors on it.

For you to modify that, you'd be going against the way the routes work in Rails and so your solution would be kind of hacky. This does tend to feel a little weird in development when you're testing things, but when the app is live, you'll never notice it because you won't be typing in URLs direct hardly ever.

Does that make sense?

Hi Chris,
My issue here is that I have a separate layout for the login, password reset and sign up etc. So when this errors its send me to a get on the /users page so shows my other layout haha. I need to work a way around that i think

Oh I gotcha, that should be much simpler.

I believe you'll need to override the Devise Registrations controller and just define the layouts in those methods.

# config/routes.rb
devise :users, controllers: { registrations: "users/registrations" }
# app/controllers/users/registrations_controller.rb
class Users::RegistrationController < Devise::RegistrationsController
    layout "new_registration", only: [:new, :create]
    layout "edit_registration", only: [:edit, :update]

Something like that should do the trick.

cheers buddy, i will look at that. I reckon thats all i needed.

You know when you look at things for so long you just miss the obvious haha

I know the feeling all too well. :)

It's all good, though, with this site I am picking up so much I am impressed with what I have made so far, and that's just the user stuff lol

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 31,575+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.