How to make Devise redirect after confirmation

后端 未结 6 806
囚心锁ツ
囚心锁ツ 2020-12-02 13:07

How can I create an after-confirmation redirect in Devise?

Before I added the confirmation module the custom after_sign_up_path worked fine

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 13:52

    Essentially, you want to change around line 25 of Devise's ConfirmationsController.

    This means you need to override the show action modifying the "happy path" of that if statement in the show action to your heart's content:

    class ConfirmationsController < Devise::ConfirmationsController
      def new
        super
      end
    
      def create
        super
      end
    
      def show
        self.resource = resource_class.confirm_by_token(params[:confirmation_token])
    
        if resource.errors.empty?
          set_flash_message(:notice, :confirmed) if is_navigational_format?
          sign_in(resource_name, resource)
          respond_with_navigational(resource){ redirect_to confirmation_getting_started_path }
        else
          respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render_with_scope :new }
        end
      end
    end
    

    And a scoped route for it (I put the view and action in the registrations controller but you can change it to whatever):

    devise_for :users, controllers: { confirmations: 'confirmations' }
    devise_scope :user do
      get '/confirmation-getting-started' => 'registrations#getting_started', as: 'confirmation_getting_started'
    end
    

    The default show action is referring to the protected after_confirmation_path_for method, so as another option, you could just modify what that method returns.

提交回复
热议问题