cancan
cancan copied to clipboard
Access different Model from a controller
suppose I have a controller A, and an action b in there. from b i access an account by params. But I have to check this account belongs to this user or not. What I have to write in ablity class. I have tried many things. but failed. for example can :b , Account do |portfolio| portfolio && portfolio.user_id == user.id end any suggestions ?
Here is the way to go:
can :b, user_id: user.id
Appropriate docs section https://github.com/ryanb/cancan/wiki/Defining-Abilities
BTW: I believe you should ask such question on StackOverflow with cancan
tag.
BTW2: This gem is not maintained anymore, use cancancan instead.
Sorry I failed to make you understand the scenario. I have a controller PortfolioGenerator. Here is a mehtod portfolio. here get a account_id as a param. Account is a different model. So I have to access this model resources. in account table there is a user_id column also.
So what should I write?
can :portfolio, user_id: user.id () if I write this how could cancan know it is for Account model ? and :portfolio is an action of PortfolioGenerator Controller ?
It would be better if you clean up your code(related models and controller) and post a gist.