简体   繁体   中英

Devise Redirect to Page After Sign In Admin

I am on Rails 4.

I have it setup so when a user signs up or logs in (devise) it will redirect to the page they were last on. It works very nicely.

However, I am having trouble figuring out how to ignore the admin completely. When signing in as an admin (admin is its own model), I receive a redirect loop, as it wants to go back to the previous page and the admin panel at the same time.

Here is my application_controller:

after_filter :store_location

  def store_location
    # store last url as long as it isn't a /users path
    session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/ 
  end

  def after_sign_in_path_for(resource)
    session[:previous_url] || root_path
  end

What would be the best way to ignore the after_sign_in_path_for if it is an admin signing in?

We need to supply on route or another since we creating such a method in our application_controller. The simple solution would be the one below.

def after_sign_in_path_for(resource)
  if resource.admin? #Assuming there is such a function
    root_path
  else
    session[:previous_url] || root_path
  end
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