Reputation: 5220
I'm using Devise for user authentication and basic authentication. Let's say I have defined two reciprocal associations between Users and Cars.
has_many :cars
belongs_to :user
User A has a car with id 1, User B has a car with id 2.
What's the best practice for preventing User A from accessing the resource at /cars/2
? I could add a before_filter for :show, :edit, :update, :destroy to each controller, but that seems tedious and repetitive. Is there any way to use Devise or CanCan for this purpose?
Upvotes: 0
Views: 227
Reputation: 901
you can use CanCan for this, read more about it here https://github.com/ryanb/cancan
after adding load_and_authorize_resource
to the top of your controller
you can add the following line to your ability model to only allow the cars owner to manage it
can :manage, Car, :user_id => user.id
Upvotes: 2