overriding devise after_sign_up_path_for not working

后端 未结 7 1438
刺人心
刺人心 2020-12-15 17:48

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

相关标签:
7条回答
  • 2020-12-15 18:11

    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.

    0 讨论(0)
  • 2020-12-15 18:12

    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.

    0 讨论(0)
  • 2020-12-15 18:12

    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!

    0 讨论(0)
  • 2020-12-15 18:15

    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

    0 讨论(0)
  • 2020-12-15 18:22

    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.

    0 讨论(0)
  • 2020-12-15 18:28

    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)
    
    0 讨论(0)
提交回复
热议问题