2010-08-16 105 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返回的数据。 让我知道它是否适合你。