2010-08-16 104 views
2

我目前正在嘗試使用活動資源與第三方API進行集成。活動資源嵌套路徑

我完成了大量的工作,但我正在努力與一個單一的嵌套資源。

/company/:company_id/users/:id 

我可以檢索使用

API :: Company.find(124343),該公司的用戶。用戶

但隨後給用戶的任何更改將不保存。

我知道我必須使用Base.site屬性來接受參數,我無法找到如何設置屬性。例如,在用戶記錄中,它具有一個company_id值。因此,獲得的company_id是容易的,我只是不知道如何獲得URL正確,因此包含它的不會正確的路線,而不是去某個地方像

/company//users/32435 
+0

你可以包括你目前如何生成的網址? – mark 2010-08-16 11:03:30

+0

我創建了一個新類,並從ActiveResource :: Base(Base 2010-08-16 11:40:45

+0

嘿,你會介意編輯你原來的帖子,逐字拷貝routes.rb文件,也是生成url的表單。 – Trip 2010-08-18 20:34:50

回答

1

試試這個

Class ABC 
require "rubygems" 
#This code block just makes sure not to append .json to the call 
class << self 
    def element_path(id, prefix_options = {}, query_options = nil) 
    prefix_options, query_options = split_options(prefix_options) if query_options.nil? 
    "#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}" 
    end 

    def collection_path(prefix_options = {}, query_options = nil) 
    prefix_options, query_options = split_options(prefix_options) if query_options.nil? 
    "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}" 
    end 
end 

#Ur site url 
ActiveResource::Base.site = 'http://aaa:8080/' 
self.format = :json 
self.collection_name= "/company/" 

def self.find(company_id, id) 
    x = superclass.find(:all, :from => '/company/%s/users/%s' %[company_id,id]) 
    x 
    end 
end 

在你的控制器,你會做

@test=ABC.find(params[:customer_id],params[:id]) 

,這會從API返回的數據。 讓我知道它是否適合你。