2012-04-13 23 views
0

我發現幾個網站,指向使用下面的代碼添加自定義參數格式的支持:的ActionController :: Base.param_parsers替代

ActionController::Base.param_parsers[Mime::PLIST] = lambda do |body| 
    str = StringIO.new(body) 
    plist = CFPropertyList::List.new({:data => str.string}) 
    CFPropertyList.native_types(plist.value) 
end 

這裏這一個是爲蘋果的plist格式,這是我我正在尋找。但是,使用Rails 3.2.1,dev服務器不會啓動,說param_parsers未定義。我無法找到任何關於它被棄用的文檔或使用其他替代方法,只是它確實包含在2.x文檔中,而不是3.x文檔中。

在Rails 3中有沒有其他方法來支持POST和PUT請求中的自定義參數格式?

回答

1

Params解析移到Rack中間件。現在是part of ActionDispatch

註冊新的解析器,你可以重新聲明使用了中間件,像這樣:

MyRailsApp::Application.config.middleware.delete "ActionDispatch::ParamsParser" 
MyRailsApp::Application.config.middleware.use(ActionDispatch::ParamsParser, { 
    Mime::PLIST => lambda do |body| 
    str = StringIO.new(body) 
    plist = CFPropertyList::List.new({:data => str.string}) 
    CFPropertyList.native_types(plist.value) 
    end 
}) 

,或者你可以更改包含像這樣

ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::PLIST] = lambda do |body| 
    str = StringIO.new(body) 
    plist = CFPropertyList::List.new({:data => str.string}) 
    CFPropertyList.native_types(plist.value) 
end 

第一個變種默認解析器不斷可能是最乾淨的。但是你需要知道,取代中間件聲明的最後一個在那裏勝出。

相關問題