Sample post to check a syntax highlighting plugin. This is just some sample code from a recent tutorial I wrote. Pretty common run-of-the-mill application_controller stuff.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | class ApplicationController < ActionController::Base protect_from_forgery protected # Returns the currently logged in user # OR nil if there isn't one def current_user return unless session[:user_id] @current_user ||= User.find_by_id(session[:user_id]) end # Make current_user available in views as a helper helper_method :current_user # Filter method to enforce a login requirement # Apply as a before_filter on any controller # you want to protect def authenticate logged_in? ? true : access_denied end # Predicate method to test for a logged in user def logged_in? current_user.is_a? User end # Make logged_in? available in templates as a helper helper_method :logged_in? def access_denied redirect_to login_path, :notice => "Please log in to continue" and return false end end |