definitialize(user, staff = nil) alias_action :read, :update, to::use# alias
if user.present? # additional permissions for logged in users (they can read their own posts) can [:new, :create], Forum cannot [:new], Comment basic_read_only else if user.admin? # additional permissions for administrators can :manage, :all elsif staff && staff.role eval("#{staff.class.name.underscore}_ability(staff)") # eval 去 call 底下的方法 else can :read, Post end end end
protected
defbasic_read_only can :read, Forum end
# 定義 Teacher 所擁有的權限 defteacher_ability(teacher) ... end
# 定義 Student 所擁有的權限 defstudent_ability(student) ... end end
classProductsController < ActionController::Base load_and_authorize_resource defdiscontinue # Automatically does the following: # @product = Product.find(params[:id]) # authorize! :discontinue, @product end end