简体   繁体   中英

Rails Devise redirecting authenticated user to a specific path

Problem

With Devise the user should be redirected to the edit_user_registration_path of the Devise controller registrations#edit action when already logged in.

Description

In the routes.rb the root is set as root :to => 'main#welcome' . The User is allways redirected to this page, even when already logged in.

Thanks a lot Fabrizio

Devise provides the user_signed_in? method.

This would be the welcome method in you main_controller.rb

def welcome 

    if user_signed_in?
        redirect_to edit_user_registration_path
    else
        // put other page in here
    end        
end

Maybe what you are looking for is a way to redirect the user to some place after he logs in. This should do it. Put this in app/controllers/application_controller.rb and place in the path you want to take the user to

  def after_sign_in_path_for(resource)
    edit_user_registration_path # or any other path needed
  end

It could be misleading, but the user is signed in by devise after sign up method called inside Devise::RegistrationsController#create

class Devise::RegistrationsController < DeviseController

  # POST /resource
  def create
    build_resource(sign_up_params)
    # omitted code
    resource.save
    sign_up(resource_name, resource)

which sign_in() the user

def sign_up(resource_name, resource)
  sign_in(resource_name, resource)
end

so the method to be enhanced is

def after_sign_in_path_for(resource)
  edit_user_registration_path
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM