5

我有一个控制器

load_and_authorize_resource :product ,:through => :current_user

...

def set_user_product
  @user_product = User::Product.find_by_id(params[:id])
end

我的ability.rb文件包含

can :edit, User::Product do |product|
  product.try(:user) == user
end

一切看起来都不错,但是当我在 url 中输入另一个用户的产品 ID 时,我得到了

ActiveRecord::RecordNotFound in User::ProductsController#edit

Completed 404 Not Found in 23ms

ActiveRecord::RecordNotFound (Couldn't find User::Product with 'id'=<another_user_product_id> [WHERE `products`.`user` = ?]):

activerecord (4.2.0) lib/active_record/relation/finder_methods.rb:336:in `raise_record_not_found_exception!'

在此处输入图像描述

如何解决从 cancan 引发的此错误

4

0 回答 0