Ruby on Rails中的ActiveResource使用详解
当HTTP响应是一个与存在的格式不同的格式时(XML和JSON),需要某些额外的格式解析,创一个你惯用的格式,并在类别中使用它。惯用的格式应当实作下列方法:extension,mime_type,
encode以及decode。
moduleActiveResource moduleFormats moduleExtend moduleCSVFormat extendself defextension 'csv' end defmime_type 'text/csv' end defencode(hash,options=nil) #数据以新格式编码并返回 end defdecode(csv) #数据以新格式解码并返回 end end end end end classUser<ActiveResource::Base self.format=ActiveResource::Formats::Extend::CSVFormat ... end
若HTTP请求应当不扩展发送时,覆写ActiveResource::Base的element_path及collection_path方法,并移除扩展的部分。
classUser<ActiveResource::Base
...
defself.collection_path(prefix_options={},query_options=nil)
prefix_options,query_options=split_options(prefix_options)ifquery_options.nil?
"#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
end
defself.element_path(id,prefix_options={},query_options=nil)
prefix_options,query_options=split_options(prefix_options)ifquery_options.nil?
"#{prefix(prefix_options)}#{collection_name}/#{URI.parser.escapeid.to_s}#{query_string(query_options)}"
end
end
如有任何改动网址的需求时,这些方法也可以被覆写。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下