Skip to content

Instantly share code, notes, and snippets.

@egrouse
Created January 24, 2012 15:52
Show Gist options
  • Save egrouse/1670798 to your computer and use it in GitHub Desktop.
Save egrouse/1670798 to your computer and use it in GitHub Desktop.
Rails redirection
class AdminController < ApplicationController
# Execute the filter to authenticate users (::authenticate)
before_filter :authenticate
def user
end
def unit
end
def index
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
# Execute the filter to select logged in user
before_filter :fetch_logged_user
# Find the logged in user in DB to verify
def fetch_logged_user
unless session[:user_id].blank?
# Find the user by stored session ID
@logged_user = User.find( session[:user_id] )
end
# rescue ActiveRecord::RecordNotFound
end
# Filter employed to perform actual authentication
def authenticate
# Check if the login user is set
unless @logged_user
redirect_to login_path, :status => 401 # Redirect to the login form
flash[:msg] = 'You must be logged in to access this page'; # Set a flash message
return false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment