How do I load a nested resource using cancan when the name of the nested model differs from the controller name?
given that I have a Worker model which is nested under Farm model, how do I correctly load the Worker resource in the Workers controller (called FarmWorkersController)? I've tried this...
class FarmWorkersController < ApplicationController
load_resource :farm, :parent => true
load_resource :class => 'Worker', :through => :farm, :parent => false
# Note that :parent and :class need to be specified on the Worker resource line,
# as the name of the controller (FarmWorkersController) is different from Worker model name
end
... but I get the error
undefined method `farm_workers' for #<Farm:0xa87670c>
Note that if I define a farm_workers() getter in the Farm model which returns the collection of Workers, then I don't get the error - though the Workers collection is not 开发者_JS百科loaded for the index action. In any case, I don't want to pollute my model to make the controller authentication work.
(It shouldn't matter but I am using mongoid)
Untested, but according to the docs/code, you should be able to specify the name as the first argument to load_resource
:
load_resource :worker, :class => 'Worker', :through => :farm, :parent => false
精彩评论