使用ActiveResource,调用MyObject.find(id)会得到"self.site/self.prefix/:id.self.format“。我们需要访问"self.site/:id/self.suffix.self.format“而不是id.file_type。
ie:获取http://api_path/:id/tool.json
有没有办法为这个场景配置activeresource?我在文档中找不到太多。
发布于 2011-06-07 05:50:52
ActiveResource::Base.element_path是创建路径的方法:
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.escape id.to_s}.#{format.extension}#{query_string(query_options)}"
end我会创建一个重定义element_path的类,如下所示:
class CustomApiPath < ActiveResource::Base
def element_path(id, prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{URI.escape id.to_s}/#{collection_name}.#{format.extension}#{query_string(query_options)}"
end
end(警告:未测试),然后其他ActiveResource模型将继承自CustomApiPath,而不是ActiveResource::Base。
https://stackoverflow.com/questions/6217193
复制相似问题