黃金俠

Ruby on Rails / Rubygems / Javascript / Git

Devise 在登入或登出之後執行回乎

| Comments

需求

想要在登入或登出後執行指定的行為

官方 wiki

How To: Change the redirect path after destroying a session i.e. signing out

說明

  • SessionsController 可得知每次登入後會 redirect 到 after_sign_in_path_for 這個 helper method 所回傳的路徑, 登出則是 after_sign_out_path_for
  • 找到 Controller::Helper 定義了 after_sign_in_path_for 和 after_sign_out_path_for
  • 針對這兩個 method 進行覆寫

範例

注意要用 private, 並且傳進一參數為 model instance

app/controllers/application_controller.rb
1
2
3
4
5
6
7
8
class ApplicationController < ActionController::Base
  private

  # Overwriting the sign_in redirect path method
  def after_sign_in_path_for(resource_or_scope)
    root_path
  end
end

Comments