0

I have a Rails app which uses Devise for user sign up / authentication. Both the sign up form and the login form are at the root of my domain.

When user registration fails (for example because they enter an email address which is already taken), by default Devise redirects to /users.

How can I change that? I would like the user to be directed to /

I have implemented this successfully for a failed login attempt, with the following code:

class CustomFailure < Devise::FailureApp
  def redirect_url
    "/"
  end

  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end
end

and:

config.warden do |manager|
  manager.failure_app = CustomFailure
end

As detailed on the project's homepage.

Is there any way to extend / alter this so that failed registrations also redirect to the root of my domain?

I'm using Ruby 2.2.0, Rails 4.2.0 and Devise 3.4.1.

James Hibbard
  • 16,490
  • 14
  • 62
  • 74

2 Answers2

1

You will probably need to subclass Devise::RegistrationsController and override the create action. Just copy over the create method from here and modify the redirect on failure to save.

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController


  def create
    build_resource
    if resource.save     
        set_flash_message :notice, :inactive_signed_up, :reason => inactive_reason(resource) if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      #end
    else
      clean_up_passwords(resource)
      respond_with_navigational(resource) { render_with_scope :new }
    end
  end


    end 

# The path used after sign up for inactive accounts. You need to overwrite
# this method in your own RegistrationsController.
def after_inactive_sign_up_path_for(resource)
  new_user_session_path
end

Change your routes to tell Devise to use your controller:

# config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
Milind
  • 4,535
  • 2
  • 26
  • 58
  • Thanks for the help. That works. However, if I `redirect_to root_url` then I lose any error messages the form would have displayed. Any ideas how to get around that? The registration form is being displayed in a partial on the homepage. – James Hibbard Jun 26 '15 at 13:48
  • What about adding error to the redirection? I mean `redirect_to root_url, error: 'Registration failed'.` Is that what you want to add? Or do you mean errors like `Email is incorrect`? – ZuzannaSt Jun 26 '15 at 13:53
  • hi updated my answer by adding create action ... @JackZelig...use it and let me know – Milind Jun 26 '15 at 13:54
  • @ZuzannaSt: I had wanted to pass the exact error message(s) and persist the form, but with your solution, at least I can pass in some kind of notification to the user. Thanks. – James Hibbard Jun 26 '15 at 14:21
  • @Milind: I guess this code is based off of Devise 3.5? I'm running Devise 3.4. I'll update over the weekend and have a play around with what you suggest. I'll let you know how I get on. – James Hibbard Jun 26 '15 at 14:24
  • Great...please accept my answer if it helped you :) @JackZelig – Milind Jun 26 '15 at 14:43
  • Hi, I updated to the latest Devise. Unfortunately, with your code I get `undefined method `render_with_scope' for #` – James Hibbard Jun 29 '15 at 08:19
  • 1
    I ended up using, passing `resource.errors.full_messages` back to the view to display the appropriate errors. That solves my problem. – James Hibbard Jun 29 '15 at 08:30
  • Good to hear that ... @JackZelig – Milind Jun 29 '15 at 09:17
1

I believe you can take a look at this question. You can override Devise RegistrationsController and add your redirect_to method to an else when User is not saved.

For example:

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
  def new
    super
  end

  def create
    if @user.save?
      #something
    else
      redirect_to your_path, error: 'Registration failed'
  end
Community
  • 1
  • 1
ZuzannaSt
  • 375
  • 1
  • 13