might want to extract this logic out into the controller. Also, look into using the before_filter to ensure that people are logged in before they can view restricted pages