创建新对象期间的康康能力
我有以下内容:
User has_many Listings
Listing has_many Offers
非常基本。用户可以发布列表,其他用户可以对这些列表进行报价。
在每个列表视图上,您可以看到列表的描述,以及它收到的所有报价的列表。此外,在同一视图中,还有一个表单可让用户对此列表提出报价。
当然,如果我正在查看自己的列表,我不希望能够提出报价。因此,我尝试对优惠的创建方法添加限制,仅允许来自与发布此列表的用户不同的用户访问它。在我的能力类中,它会是这样的:
can :create, Offer if listing.user != user
这不起作用,因为 listing
没有在任何地方定义。当用户查看某个listing
时会调用此方法,那么,如何将当前的Listing传递给cancan方法来有效检查此限制呢?
谢谢。
I have the following:
User has_many Listings
Listing has_many Offers
Pretty basic. A user can publish listings and other users can make offers on those listings.
On each Listing View, you can see a description of the listing, and also a list of all of the offers it has received. Also, on this same view, there is a form which lets users make an offer on this Listing.
Of course, if I am looking at my own Listing, I do not want to be able to make an Offer. So, I'm trying to add a restriction on the Offer's create method, to only allow access to it if it is from a User different from the one that posted this Listing. In my Ability class, it would be something like:
can :create, Offer if listing.user != user
This doesn't work since listing
is not defined anywhere. This method will be called when a user is viewing a certain listing
, so, how can I pass this current Listing to the cancan method to effectively check this restriction?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 CanCan 能力检查移至创建新报价之后。例如
Move the CanCan ability check until after the point at which you've created the new offer. E.g.