overriding devise after_sign_up_path_for not working

China☆狼群 提交于 2019-11-29 03:02:38

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

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.

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

Andy R.

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.

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.

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)

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!