role.rb
enum role: {
writer: 0,
editor: 10,
admin: 20
}
ちなみにuser.rbにも同じように記載したら 以下のエラーが発生した。
You tried to define an enum named "role" on the model "User",
but this will generate a instance method "admin?", which is already defined by another enum.
意味は重複しとるよっていうことやと思う。
Pundit
リソースに対して、どのユーザーであれば処理が許可されるのかを定義するgem。
punditのgemをインストールして、以下のような記載をする。
application controller
class ApplicationController < ActionController::Base
include Pundit
end
次にrails g pundit:installをする。 policyファイルを生成してくれる。
Policyファイルに認可を与えるユーザーについて設定していきます。
今回は、adminとeditorに認可を与えることにします。