方法1.在controller中做
用beforeaction
使用範圍比較侷限。當然也可以放在application controller中增加使用範圍。
controller中
before_action :require_is_admin
privite
def require_is_adminif current_user.email != 'lisa@google.com'
flash[:alert] = 'You are not admin'
redirect_to root_path
end
end
方法2.在model中做
一開始使用範圍就比較廣了,因為可以取用model的地方很多。
user model>>
def admin?
email == 'lisa@google.com'
end
controller>>
unless current_user.admin?
flash[:alert] = 'You are not admin'
redirect_to root_path
end
controller>>
def admin_required
if !current_user.admin?
redirect_to "/", alert: "You are not admin."
end
end
controller>>
def admin_required
if !current_user.admin?
redirect_to "/", alert: "You are not admin."
end
end
沒有留言:
張貼留言