我正在使用 ActiveResource 来管理访问外部服务。
外部服务的 URL 如下:
http://api.cars.com/v1/cars/car_id/range/range_num?filter=filter1,filter2
这是我的汽车课:
class Car < ActiveResource::Base
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}/#{URI.parser.escape id.to_s}#{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
self.site = "http://api.cars.com/"
self.prefix = "/v1/"
self.format = :json
end
当我设置对象以在 rails 控制台中获取特定汽车时:
> car = car.new
> car.get('1234')
我得到一个这样的网址:
http://api.cars.com/v1/cars//1234.json
如何让 URL 包含 range 和 range_num 元素?
另外,我不希望 URL 末尾的 .json 扩展名。我已经尝试覆盖 element_name 和 collection_name 方法,如下所述:How to remove .xml and .json from url when using active resource但它似乎对我不起作用......
提前感谢您的任何想法!