kareem-h
9/9/2014 - 6:53 PM

redirect to previous page

redirect to previous page

class ApplicationController < ActionController::Base
  
  private

  def store_referrer_location
    session[:return_to] = request.referrer
  end

  def redirect_back_or_default(default)
    redirect_to(session.delete(:return_to) || default)
  end

end

# user_sessions_controller.rb
class UserSessionsController < ApplicationController
  def new
    store_referrer_location if session[:return_to].blank?
    @user_session = UserSession.new
    @header_title = "Login"
  end

  ...

  def destroy
    store_referrer_location if session[:return_to].blank?
    current_user_session.destroy
    flash[:success] = "Logout successful!"
    redirect_back_or_default root_url
  end
end