I'm doing this in the User#Show view:
<% if policy(Gallery.new).create? %>
<%= link_to "Add a photo gallery for #{@user.name}", new_user_gallery_path(@user), class: 'btn btn-success' %>
<% end %>
and Admin can add galleries to any user. But nobody else can.
Here's the Gallery Policy:
class GalleryPolicy < ApplicationPolicy
def create?
user.present? && (record.user == user || user.admin?)
end
def new?
create?
end
end
Here's the Application Policy:
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
def index?
true
end
def show?
scope.where(:id => record.id).exists?
end
def create?
user.present?
end
def new?
create?
end
def update?
user.present? && (record.user == user || user.admin?)
end
def edit?
update?
end
def destroy?
update?
end
def scope
record.class
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
scope
end
end
end
As you can see, a user should be logged in and the record should belong to them or they should be admin for them to create galleries. What am I doing wrong?