How i can redirect to different signout paths usin

2019-08-07 02:29发布

问题:

I am using rails3 and gem devise and i have two roles admin and customer and i want after user

sign out admin should redirect to different path and customer should redirect to different path

when sign out..

回答1:

You can get your desired functionality by using devise method after sign_out path.

but before you define these methods in application helper.

def is_admin?(user)
  admin_role = Role.find(:first, :conditions => ["name = ?", "admin"])
  return user.roles.include?(admin_role)
end


def is_customer?(user)
  admin_role = Role.find(:first, :conditions => ["name = ?", "customer"])
  return user.roles.include?(admin_role)
end

After that include application helper in application controller and define this method

def after_sign_out_path_for(resource_or_scope)
  if is_admin?(current_user)
    home_path = "/admin/users/sign_in"
  elsif is_customer?(current_user)
    home_path = "/customer"
  end
    respond_to?(home_path, true) ? send(root_path) : home_path

end

Hope it will work fine !!!



回答2:

You can override the after_sign_out_path_for(resource) method that devise uses. Just mention the logic or just the desired redirection path after checking the roles of your user in the method. The devise session's destroy action calls the redirection path through this method.

def after_sign_out_path_for(resource)
  #logic
end

Hope this is helpful..



回答3:

The Devise README covers how to set up custom routes and controllers. In short you'll want to customize your routes for each model, e.g.:

devise_for :admins,    :controllers => { :sessions => "admins/sessions" }
devise_for :customers, :controllers => { :sessions => "customers/sessions" }

Then create the corresponding controllers and override Devise::SessionsController#destroy, e.g.:

class Admins::SessionsController < Devise::SessionsController
  def destroy
    super

    redirect_to ...
  end
end