In my application using CanCan I have permissions where users can view and create stores but I also want them to only be able to edit the ones they’ve created. Users can create as many stores as they like, which all should be editable by them. A store doesn’t have users so how could I do this when theirs no user_id apart of the Store table?
CanCan:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.role == "default"
can :read, Store
can :create, Store
end
end
end
Since a user will be able to create as many stores as they like, a store will belong to a user.
You MUST create this relationship.
So, in the
Usermodel.And in the
Storemodel.And in the
ability.rbfile, just put something like: