Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Alan Reid asked in Gems / Libraries

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

Alan

Reply

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?

Reply

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

Reply

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]
end

Something like that should do the trick.

Reply

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

Reply

I know the feeling all too well. :)

Reply

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

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.