27

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"
Cœur
  • 37,241
  • 25
  • 195
  • 267
katie
  • 2,921
  • 8
  • 34
  • 51

7 Answers7

95

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.

Ryan McCuaig
  • 2,229
  • 15
  • 21
  • Thank you, that was my issue. – MicFin Jun 24 '14 at 04:15
  • Thanks a lot! was having this issue too :D – marman Oct 05 '14 at 21:59
  • 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. – Paul Odeon Sep 17 '15 at 12:32
  • 4
    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`). – sameers Feb 23 '17 at 09:08
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

Ryan Francis
  • 635
  • 6
  • 7
  • Even thought the OP question might have been served better by another answer, this one was the problem I was having! – lfzawacki Sep 18 '14 at 20:55
7

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.

Community
  • 1
  • 1
Andy R.
  • 199
  • 2
  • 13
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

Elliot
  • 2,199
  • 4
  • 21
  • 28
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)
yanyingwang
  • 117
  • 6
  • 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`. – Franchy Oct 14 '20 at 14:11
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.

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
0

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!

BenKoshy
  • 33,477
  • 14
  • 111
  • 80