Ruby on Rails中的ActiveResource使用詳解
當(dāng) HTTP 響應(yīng)是一個(gè)與存在的格式不同的格式時(shí)(XML 和 JSON),需要某些額外的格式解析,創(chuàng)一個(gè)你慣用的格式,并在類別中使用它。慣用的格式應(yīng)當(dāng)實(shí)作下列方法:extension, mime_type,
encode 以及 decode。
module ActiveResource
module Formats
module Extend
module CSVFormat
extend self
def extension
'csv'
end
def mime_type
'text/csv'
end
def encode(hash, options = nil)
# 數(shù)據(jù)以新格式編碼并返回
end
def decode(csv)
# 數(shù)據(jù)以新格式解碼并返回
end
end
end
end
end
class User < ActiveResource::Base
self.format = ActiveResource::Formats::Extend::CSVFormat
...
end
若 HTTP 請求應(yīng)當(dāng)不擴(kuò)展發(fā)送時(shí),覆寫 ActiveResource::Base 的 element_path 及 collection_path 方法,并移除擴(kuò)展的部分。
class User < ActiveResource::Base
...
def self.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
def self.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
end
如有任何改動(dòng)網(wǎng)址的需求時(shí),這些方法也可以被覆寫。
相關(guān)文章
windows下安裝ruby與rails時(shí)遇到的問題總結(jié)
這篇文章主要總結(jié)了windows下安裝ruby與rails時(shí)遇到的問題,本文總結(jié)的問題都是通過自己實(shí)踐所得來的,總結(jié)的還算是相對比較全面,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。2017-04-04
Ruby實(shí)現(xiàn)的最優(yōu)二叉查找樹算法
這篇文章主要介紹了Ruby實(shí)現(xiàn)的最優(yōu)二叉查找樹算法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05
Luhn算法學(xué)習(xí)及其Ruby版實(shí)現(xiàn)代碼示例
Luhn算法主要北用來進(jìn)行數(shù)字驗(yàn)證,尤其是卡號身份證號等,這里我們就來看一下Luhn算法學(xué)習(xí)及其Ruby版實(shí)現(xiàn)代碼示例:2016-05-05
比較詳細(xì)的ruby symbol 學(xué)習(xí)資料
最近在學(xué)習(xí)ruby on rails,的確是一個(gè)優(yōu)秀的數(shù)據(jù)庫開發(fā)框架。但在過程中,發(fā)現(xiàn)在視圖文件夾中的rhtml文件里有大量的類似于以下的語句2008-08-08

