Skip to content

Instantly share code, notes, and snippets.

@TakuyaHarayama
Last active June 7, 2017 16:16
Show Gist options
  • Save TakuyaHarayama/c31a3e0e17e5b0ecdfd60b0402ead80f to your computer and use it in GitHub Desktop.
Save TakuyaHarayama/c31a3e0e17e5b0ecdfd60b0402ead80f to your computer and use it in GitHub Desktop.
前のページに戻るアクション
class ApplicationController < ActionController::Base
before_action :set_request_from
# どこのページからリクエストが来たか保存しておく
def set_request_from
if session[:request_from]
@request_from = session[:request_from]
end
# 現在のURLを保存しておく
session[:request_from] = request.original_url
end
# 前の画面に戻る
def return_back
if request.referer
redirect_to :back and return true
elsif @request_from
redirect_to @request_from and return true
end
end
end
# 各controllerで呼ぶ場合
return_back and return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment