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
如有任何改动网址的需求时,这些方法也可以被覆写。
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短