Overriding Devise's registration controller to allow for a redirect after a successful sign_up has been done

后端 未结 2 510
心在旅途
心在旅途 2021-02-02 01:52

I have looked all over the place, and found a lot of info... but nothing works for me and I don\'t get it :(

I know that you are suppose to override the registration con

2条回答
  •  时光说笑
    2021-02-02 02:51

    Ok... I am able to override it so you should be either :0

    Create folder app/controllers/users

    put there registrations_controller.rb with: (option with session - but it will try sign_in and later redirect - it may be not intended behavior for you ). Furthermore this is from devise wiki and I am not sure if it works

    class Users::RegistrationsController < Devise::RegistrationsController
    
      def create
        session["#{resource_name}_return_to"] = complete_path
        super
      end
    
    end
    

    restart application (just for ensure you don't trust anything)


    All in all you must override Create If you want redirect only Users... if you want define some more complex scenario you should monkeypatch sign_in_and_redirect

    so your controller will looks like

    class Users::RegistrationsController < Devise::RegistrationsController
      # POST /resource/sign_up
      def create
        build_resource
    
        if resource.save
          set_flash_message :notice, :signed_up
    
          #sign_in_and_redirect(resource_name, resource)\
          #this commented line is responsible for sign in and redirection
          #change to something you want..
        else
          clean_up_passwords(resource)
          render_with_scope :new
        end
      end
    end
    

    second option try to monkeypatch helper ....

    module Devise
      module Controllers
        # Those helpers are convenience methods added to ApplicationController.
        module Helpers
          def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false)
            #intended behaviour for signups
          end
        end
      end
    end
    

提交回复
热议问题