samedi 25 mars 2017

Override redirect on error in Devise registrations controller (Rails)

To add some custom logic to my sign up flow, I have overriden the default Devise registrations controller like this:

class RegistrationsController < Devise::RegistrationsController
  def new
    super
  end
end

# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}

The above is working fine. Now, I am trying to create different sign up views (which are maybe routed as /corporate/sign_up OR /personal/sign_up, whose forms all submit to my registrations controller.

But by calling super inside registrations controller, devise redirects to /users if there is any validation error (e.g. email already taken), which loads my original sign up view. How do I tweak this system to redirect to the custom sign up page which initiated the call to my registrations controller?

The only solution I could find is to copy all of the devise registrations controller logic here, and just return the error and not redirect.

What is a better solution for this?

Aucun commentaire:

Enregistrer un commentaire