创建新对象期间的康康能力

发布于 2024-12-05 08:55:04 字数 495 浏览 0 评论 0原文

我有以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2024-12-12 08:55:04

将 CanCan 能力检查移至创建新报价之后。例如

#listing.rb
load_and_authorize_resource :except => :create

def create
    @offer = #whatever it is you want to do
    authorize! :create, @offer

end

Move the CanCan ability check until after the point at which you've created the new offer. E.g.

#listing.rb
load_and_authorize_resource :except => :create

def create
    @offer = #whatever it is you want to do
    authorize! :create, @offer

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文