2011-01-19 86 views
1

鑑於我有一個嵌套在Farm模型下的Worker模型,我如何在Workers控制器(稱爲FarmWorkersController)中正確加載Worker資源?我已經試過這...當嵌套模型的名稱與控制器名稱不同時,如何使用cancan加載嵌套資源?

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 

...但我得到的錯誤

undefined method `farm_workers' for #<Farm:0xa87670c> 

需要注意的是,如果我在返回工人收集的農場模型定義farm_workers()的吸氣劑,那麼我不會收到錯誤 - 儘管Worker集合未加載索引操作。無論如何,我不想污染我的模型以使控制器身份驗證正常工作。

(它不應該的問題,但我使用mongoid)

回答

3

未經檢驗的,但根據文檔/代碼,你應該能夠指定名稱的第一個參數load_resource

load_resource :worker, :class => 'Worker', :through => :farm, :parent => false
+0

謝謝安迪,我試了你的答案(用load_resource:worker代替load_resource'workers')。它似乎有效!我基本上忽略了第一個論點。謝謝! – 2011-01-19 14:15:38