def index
#@tasks = Task.all
current_user.tasks.build(task_params)
authorize @tasks
end
class AddUserIdToTasks < ActiveRecord::Migration[5.1]
def change
add_reference :tasks, :user, foreign_key: true
end
end
belongs_to :user
has_many :tasks
мб надо что-то другое выбрать, а то я как сомневаюсь в has_many
authorize @tasks
в методы index, new, create, destroy (в метод edit тоже надо?)class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
raise Pundit::NotAuthorizedError, "must be logged in" unless user
@user = user
@record = record
end
def index?
false
end
def show?
scope.where(:id => record.id).exists?
end
def create?
false
end
def new?
create?
end
def update?
owner?
end
def edit?
owner?
end
def destroy?
owner?
end
def owner?
task.user == user
end
def scope
Pundit.policy_scope!(user, record.class)
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
scope
end
end
end
class TaskPolicy < ApplicationPolicy
def index?
true
end
def create?
user.present?
end
def update?
return true if user.present? && user == task.user
end
def destroy?
return true if user.present? && user == task.user
end
private
def article
record
end
end