Rails Devise使用時のフレンドリーフォワーディング
ブラウザから直接URL入力してあるページへ行こうとした時、 まだログインしていなければログイン画面を表示させる。というのはよくある処理だと思います。
そしてログイン後には、ログインする前に行きたかったところへ送ってあげるのが親切でしょう。
そういう時の Rails, Devise を使った場合のサンプルです。
class ApplicationController < ActionController::Base protect_from_forgery def after_sign_out_path_for resource root_path end def after_sign_in_path_for(resource) if (session[:user_return_to] == root_path) super else session[:user_return_to] || root_path end end end