It looks like your session contains old data, specifically a user ID (1) that no longer exists. Try to handle the RecordNotFound exception RecordNotFound by ActiveRecord and return zero:
def current_user @current_user ||= User.find(session[:user_id]) if session[:user_id] rescue ActiveRecord::RecordNotFound end
To redirect, you must add a second before_filter that validates the user and handles the redirection to the login path:
before_filter :require_user def require_user redirect_to login_path unless current_user end
Remember to skip require_user for your login action by adding skip_before_filter :require_login to which controller manages your authentication.
source share