如何使用cancan load_and_authorize_resource解决记录未找到的错误:product,:through => :当前用户

时间:2015-04-13 07:48:51

标签: 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

我的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:在`raise_record_not_found_exception!'

enter image description here

如何解决从cancan

引发的此错误

0 个答案:

没有答案