bookstore/app/controllers/application_controller.rb
2021-03-22 03:16:29 +01:00

28 lines
645 B
Ruby

# frozen_string_literal: true
# Base for application controllers
class ApplicationController < ActionController::Base
helper_method :current_user
helper_method :logged_in?
def current_user
User.find_by(id: session[:user_id])
end
def logged_in?
!current_user.nil?
end
protected
def notices_from_errors(record)
errors = record.errors.messages.map do |attribute, messages|
messages.map { |message| "#{attribute} #{message}".capitalize }
end
errors.flatten
end
def ensure_admin
redirect_to '/welcome', notice: 'You are not allowed to perform this action' unless current_user&.admin?
end
end