Devise override redirect after form submit

前端 未结 1 1452
情歌与酒
情歌与酒 2020-12-19 21:55

How can I configure my Rails app such that after the form to create a new user is submitted (through devise), I redirect to my own desired page ?

Thank you

相关标签:
1条回答
  • 2020-12-19 22:24

    After the create user form is submitted the user is created and then logged in so the page you are being redirected to is actually the after log in page. If you only want to change this page when a user is created you can set session["#{resource_name}_return_to"] in a custom registration controller like this:

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

    You can also create a root route for your user object in routes.rb which will redirect all users whenever they log in:

    match "user_root" => "users#home"
    

    Finally you can define the after_sign_in_path_for(resource_or_scope) method in your application_controller and this will allow you to conditionally redirect users:

    def after_sign_in_path_for(resource_or_scope)
      if resource_or_scope.is_a?(User)
        some_custom_path    
      else
        super
      end
    end
    
    0 讨论(0)
提交回复
热议问题