I use Devise and Cancan for rails 3.2.6. In the application, I allow users to create a document with some information collected in the form. Then I want to allow the user to list only their documents that work on the document index page on localhost: 3000 / users / 1 /. What does not work, I try to deprive the user of the ability to see all other documents by replacing / users /: id / documents with a different number.
I use cancan and tried both
can: index, Document ,: user_id => user.id can: read, Document,: user_id => user.id
and then in the document controller index method
if can? :read, Document @documents = @user.documents else redirect_to root_path end
also tried with :index , but this does not work. I also use load_and_authorize_resource ..
Any thoughts on what I'm missing?
I will say that cancan works for my user and user controller to create, edit and edit users, so I know that cancan works in general. It also works to update and delete user documents. This is just an index function not working.
class Ability include CanCan::Ability def initialize(user) user ||= User.new
source share