简体   繁体   中英

Redirect After Sign Up and Sign In with 2 Models (Devise)

I have two models in my application, User and Admin. So, I put these code in my application controller:

def after_sign_in_path_for(user)
  root_path
end

def after_sign_in_path_for(admin)
  admin_path
end

However, it only executes the last one (admin). So, whenever I signed in as user, I will be redirected to the admin_path instead of root_path as it should be. Any ideas?

Do something like this, in case you have two separate models for User and Admin :

def after_sign_in_path_for(resource)
  resource.is_a?(Admin) ? admin_path : root_path
end

Ruby understands that you are redefining after_sign_in_path_for , so the last one defined will take effect.

You can simply modify your code like this to make it work:

def after_sign_in_path_for(user)
  user.admin? ? admin_path : root_path
end

Just make sure user.admin? is the function to check whether a user has admin role or not!

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