LoginSignup
0
0

More than 5 years have passed since last update.

Rails4でアクション毎に実行権限を設定する

Posted at

以下の感じで設定をしてみました。

  • updateできればeditを、createできればnewを実行できる
  • ログインユーザ自身のレコードだけshow, update, destroyできる
  • ログインしていればcreateできる
  • ログインユーザのレコードだけindexで一覧できる
app/policies/blog/site_policy.rb
class Blog::SitePolicy < ApplicationPolicy
  def index?
    !!user
  end

  def show?
    record.user == user
  end

  def create?
    !!user
  end

  def new?
    create?
  end

  def update?
    record.user == user
  end

  def edit?
    update?
  end

  def destroy?
    record.user == user
  end

  class Scope < Scope
    def resolve
      scope.where(user_id: user.id)
    end
  end
end

よく詳しくはブログ記事に載せたコントローラ側のコードもご覧ください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0