43
41

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Deviseでログイン/ログアウト後のリダイレクト先をログイン/ログアウトする前のページにする

Posted at

###参考

How To: Redirect back to current page after sign in, sign out, sign up, update · plataformatec/devise Wiki

session[:previous_url] にいれるようにすればいい。

ApplicationController で

after_filter :store_location

def store_location
 # 今回の場合は、 /users/sign_in , /users/sign_up, /users/password にアクセスしたとき、ajaxでのやりとりはsessionには保存しない。
    if (request.fullpath != "/users/sign_in" && \
        request.fullpath != "/users/sign_up" && \
        request.fullpath != "/users/password" && \
        !request.xhr?) # don't store ajax calls
      session[:previous_url] = request.fullpath 
    end
end

#ログイン後のリダイレクトをログイン前のページにする場合
def after_sign_in_path_for(resource)
  session[:previous_url] || root_path
end

#ログアウト後のリダイレクトをログアウト前のページにする場合
def after_sign_out_path_for(resource)
  session[:previous_url] || root_path
end
43
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
41

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?