overriding devise after_sign_up_path_for not working
Asked Answered
A

7

29

In routes i have the root-path pointing "home#index" but when i try to override that with after_sign_up_path_for keeps redirecting me to the root path when I sign in or sign up. I have tried to put it in both in devise subclassed controller and application_controller, but it didn't work. What do I need to do here?

Application controller

class ApplicationController < ActionController::Base
  protect_from_forgery

  def after_sign_up_path_for(resource)
    show_cities_path(resource)
  end
end

registration controller

class RegistrationsController < ApplicationController
  def after_sign_up_path_for(resource)
    show_cities_path(resource)
  end
end

routes

root :to => "home#index"
Acton answered 3/11, 2011 at 23:44 Comment(0)
T
6

Have you checked your show_cities_path exists, by executing rake routes? Might be worth having a look at https://github.com/plataformatec/devise/wiki/How-To:-Change-the-redirect-path-after-destroying-a-session-i.e.-signing-out

Tarnetgaronne answered 4/11, 2011 at 0:20 Comment(0)
C
97

If you also have the Confirmable module enabled, you have to override after_inactive_sign_up_path_for since a new sign-up is "inactive" until it's confirmed. after_sign_up_path_for doesn't seem to get called when Confirmable is active.

Caviness answered 12/2, 2012 at 22:10 Comment(2)
Worth noting that this applies to all cases where a user is inactive for login, for instance if you override active_for_authentication? This includes the confirmable case where the user is inactive for login until they confirm their account.Cell
In fact, at the current point in time, you have to note that the method will not be over-ridden by a similarly named method in ApplicationController - you have to declare your own controller for :registrations, in which you need to insert this sole method (after_inactive_sign_up_path_for).Borstal
P
20

Although I am late to the game, I just ran into this problem and had trouble finding the solution.

If you are using your own RegistrationsController to customize Devise, then you need to add the after_sign_up_path_for(resource) method to that controller instead of ApplicationController.

In registrations_controller.rb:

private

  def after_sign_up_path_for(resource)
    new_page_path
  end

https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

Propellant answered 31/10, 2013 at 1:58 Comment(1)
Even thought the OP question might have been served better by another answer, this one was the problem I was having!Celestinacelestine
S
8

I struggled with this problem until realizing I had forgotten to declare that I was overriding devise's registrations controller. In my case, I'm using devise with the :user resource, so I added this to routes.rb:

devise_for :users, :controllers => {:registrations => "registrations"}

After that, the redirect that I specified in after_inactive_sign_up_path_for worked.

Override devise registrations controller has a more complete discussion on this topic, with alternative ways of declaring overrides.

Shahaptian answered 8/10, 2013 at 7:6 Comment(0)
T
6

Have you checked your show_cities_path exists, by executing rake routes? Might be worth having a look at https://github.com/plataformatec/devise/wiki/How-To:-Change-the-redirect-path-after-destroying-a-session-i.e.-signing-out

Tarnetgaronne answered 4/11, 2011 at 0:20 Comment(0)
E
1

Actually, we can view the source code of devise to solve the problem and it's easy.

devise-3.4.1 $ vim app/controllers/devise/registrations_controller.rb

  # POST /resource
  def create
    build_resource(sign_up_params)

    resource_saved = resource.save
    yield resource if block_given?
    if resource_saved
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_flashing_format?
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      @validatable = devise_mapping.validatable?
      if @validatable
        @minimum_password_length = resource_class.password_length.min
      end
      respond_with resource
    end
  end

As code show:

      if resource.active_for_authentication?
        ...
        respond_with resource, location: after_sign_up_path_for(resource)

      else
        ...
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
Electroballistics answered 22/4, 2015 at 20:11 Comment(1)
This answer allowed me to realize that if you are using the confirmable module you need to overwrite the after_inactive_sign_up_path_for method instead of the after_sign_up_path_for method. This is because the user won't sign in right away, it needs to confirm the email first, so Devise won't call after_sign_up_path_for.Madai
I
1

If using OmniAuth custom callback controllers with Rails 5.2:

In my particular case, the after sign up path was not working: but I was using OmniAuth with a custom callback controller, which was invoking the after_sign_in_path_for rather than the former:

def google_oauth2 @user = User.from_omniauth(request.env["omniauth.auth"])

if @user.persisted?
  # Here's the guilty line your honour:
  sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
  set_flash_message(:notice, :success, kind: "Google") if is_navigational_format?
else
  session["devise.google_oauth2_data"] = request.env["omniauth.auth"]
  redirect_to new_user_registration_url
end

end

........And the sign_in_and_redirect path redirects to the after_sign_in_path_for method. So I generated a new devise controller for sessions and simply overrode that method. problem solved!

Isobelisocheim answered 18/9, 2018 at 9:55 Comment(0)
Y
0

I've just blown about 2 hours on this, but LiveReload was my issue. I was being redirected successfully but LiveReload was picking up the change on development.sqllite and overriding the request.

Yakutsk answered 4/1, 2014 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.