Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 可以为嵌套资源加载资源don';t build有一个关联_Ruby On Rails_Cancan - Fatal编程技术网

Ruby on rails 可以为嵌套资源加载资源don';t build有一个关联

Ruby on rails 可以为嵌套资源加载资源don';t build有一个关联,ruby-on-rails,cancan,Ruby On Rails,Cancan,我有一个拥有者模型,它有一个地址,并接受嵌套的属性。加载所有者时,对于:new操作,我希望:load_resource方法可以像@Owner.build_address那样构建关联,但下面的代码不会这样: class OnwersController < ApplicationController load_and_authorize_resource load_resource :address, :through => :owner, :singleton =&g

我有一个拥有者模型,它有一个地址,并接受嵌套的属性。加载所有者时,对于:new操作,我希望:load_resource方法可以像@Owner.build_address那样构建关联,但下面的代码不会这样:

class OnwersController < ApplicationController
    load_and_authorize_resource
    load_resource :address, :through => :owner, :singleton => true, :parent => false
class OnwersController:owner,:singleton=>true,:parent=>false
这是预期的行为吗?我必须自己做@owner.address=@address吗


谢谢

您可以参考答案


它对我有效,只是我必须同时忽略:新建和:创建操作。

我和你有同样的问题。似乎CanCan可以用“Class.new”来构建资源。所以它不知道两者的关系。谢谢你的回答@gfreezy,但我认为我们所处的环境略有不同。在您的示例中,控制器必须通过对象加载自己的模型。在我的OwnersController中,我想从所有者模型加载其他模型(地址),这就是为什么我在加载地址时使用:parent=>false。请注意,我已经使用了:through选项。我将这行代码解释为:load_resource:address(加载地址),:through=>:owner(对于owner对象),:singleton=>true(仅一个地址),:parent=>false(这不是AddressController)