RBOnRails-learning/app/controllers/application_controller.rb

19 lines
465 B
Ruby

class ApplicationController < ActionController::Base
helper_method :current_user, :logged_in?
def current_user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
end
def logged_in?
!current_user.nil?
end
def require_user
unless logged_in?
flash[:alert] = "You must be logged in first. Please visit <a href=\"/signup\">the signup page</a> to create an account."
redirect_to login_path
end
end
end