4

I have three possible permissions for a User to be in my Rails app, they are User.is_admin, User.is_school, and User.is_security. Based on the nature of my app I need to have a separate home screen for each of these users that do radically different things, which I have working. The problem that I'm having has to do with how Devise auto redirects to root_path after login for all users, regardless of the permissions I have set.

I generated the Devise Sessions controllers into the Users namespace and I have overwritten it to default to my controller, but now when I try to do a redirect, based on the conditional permissions, I get a DoubleRenderError (The obvious reason being that Devise is redirecting elsewhere when creating the session).

I have tried running it as an after_action and even tried overwriting the after_sign_in_path_for method, as per the direction of the Devise docs on the matter, but I still can't get it working. Any help would be appreciated, thank you!

MZaragoza
  • 10,108
  • 9
  • 71
  • 116
Zubatman
  • 1,235
  • 3
  • 17
  • 34
  • 3
    "I have tried running it as an after_action and even tried overwriting the after_sign_in_path_for method, as per the direction of the Devise docs on the matter, but I still can't get it working." You need to post your code, because this is how to get what you want from Devise. Just remember, we don't have magic powers. If you expect help, you need to tell us exactly what you tried. – MarsAtomic Aug 15 '15 at 01:11
  • did you get a DoubleRenderError because you added a redirect while `after_sign_in_path_for(resource)` also redirects? ya, might be time to start posting some code – daslicious Aug 15 '15 at 03:04

1 Answers1

7

You can do something like this

class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
    if resource.role == 'admin'
      admin_root_path
    else
      user_root_path
    end
  end
end

you can read more about this https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in

MZaragoza
  • 10,108
  • 9
  • 71
  • 116