2014-03-19 52 views
1

我有幾個與belongs_to關係的模型。該機型都有定製to_param方法設置爲使用資源的關鍵,而不是實際的IDActiveAdmin嵌套自定義資源檢索

def to_param 
    return self.resource_key 
end 

對我的管理模式,我有:

ActiveAdmin.register Foo do 

    controller do 
    def find_resource 
     Foo.find_by(resource_key: params[:id]) 
    end 
    end 

    panel "Bars" do 
    table_for foo.bars do 
    column "Title" do |bar| 
     link_to bar.title, admin_foo_bar_path(foo, bar) 
    end 
    end 
end 

end 

ActiveAdmin.register Bar do 
    belongs_to :foo 

    controller do 
    def find_resource 
    Bar.find_by(resource_key: params[:id]) 
    end 
    end 
end 

美孚正常工作,各個環節都與生成URL路徑中的resource_key。正確的網址生成酒吧,爲好,但是當我嘗試打開欄項目我得到一個消息,如: 找不到美孚與ID = {} RESOURCE_ID

我其實並不需要在我的Bar視圖中的Foo值,Bar資源鍵都有足夠的數據來查詢。我需要告訴應用程序不要嘗試查找Foo值,或者將Bar設置爲通過resource_key而不是id正確查詢Foo。

我使用Rails 4與AA的1.0主分支。

+0

您的路線是如何定義的?你可以分享他們的問題嗎? –

+0

對於AA,我只是使用默認的'ActiveAdmin.routes(self)' – jschweitzer

回答

2

兩個可能的修復

1)嘗試在belongs_to的語句中使用可選的

belongs_to :foo, :optional => true #it gives you urls for Bar without Foo 

2)AA使用Inherited_resources寶石,嘗試自定義belongs_to的(默認情況下它使用由ID找到)

來自inherited_resources的示例

belongs_to accepts several options to be able to configure the association. For example, if you want urls like "/projects/:project_title/tasks", you can customize how InheritedResources find your projects: 

class TasksController < InheritedResources::Base 
    belongs_to :project, :finder => :find_by_title!, :param => :project_title 
end 

所以這應該是他lp

belongs_to :foo , :finder => :find_by_resource_key! 
+0

謝謝,我選擇了第二個選項,它工作得很好。 – jschweitzer