Ruby on rails 如何使用cancan load_和授权解决记录未找到错误资源:product,:through=>;:当前用户

Ruby on rails 如何使用cancan load_和授权解决记录未找到错误资源:product,:through=>;:当前用户,ruby-on-rails,cancan,cancancan,Ruby On Rails,Cancan,Cancancan,我有一个控制器 load_and_authorize_resource :product ,:through => :current_user ... def set_user_product @user_product = User::Product.find_by_id(params[:id]) end 我的能力.rb文件包含 can :edit, User::Product do |product| product.try(:user) == user end 一切看

我有一个控制器

load_and_authorize_resource :product ,:through => :current_user

...

def set_user_product
  @user_product = User::Product.find_by_id(params[:id])
end
我的
能力.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::RecordNotFound in User::ProductsController#edit
23ms内未找到完成的404
ActiveRecord::RecordNotFound(找不到id为[WHERE`products`.`User`=?]的用户::产品):
activerecord(4.2.0)lib/active\u record/relation/finder\u methods.rb:336:in'raise\u record\u not\u found\u exception!'


如何解决由cancan

在controllerBut id中使用
rescue ActiveRecord::RecordNotFound
引发的此错误,但我想显示当前用户cancan access denied错误。