2013-08-28 72 views
0

這感覺應該很簡單,但谷歌搜索一個小時後,我無法弄清楚這一點。Rails 4:阻止'to_json'添加根節點?

我將亞馬遜S3'策略文檔'作爲JSON發佈到我的服務器。 我需要編碼JSON ,就像一樣,但Rails正在向'params'添加東西,這使得我需要編碼的JSON變得混亂。

以下是我有:

class Api::Amazons3Controller < Api::BaseController 

def sign_policy 
    policy_document = params.except(:action, :controller) 
    encoded_policy_document = Base64.encode64(policy_document.to_json).gsub(/\n|\r/, '') 
    signature = Base64.encode64(
    OpenSSL::HMAC.digest(
     OpenSSL::Digest::Digest.new('sha1'), 
     ENV['AWS_SECRET_ACCESS_KEY'], 
     policy_document) 
    ).gsub(/\n/, '') 

    response = { policy: policy_document, signature: signature } 
    render json: response 
end 
end 

我想 '清理' 與params.except的PARAMS(:行動:控制器),但policy_document.to_json增加了在JSON周圍的根名稱叫做'amazons3'(控制器名稱),這是我不想要的。我只需要從請求中編碼純json。

任何幫助,將不勝感激!

+1

你的答案在這裏[http://stackoverflow.com/questions/6515436/rails-3-1-include-root-in-json][1] [1]:http://stackoverflow.com/questions/6515436/rails-3-1-include-root-in-json – techvineet

+0

謝謝你指着我wrap_parameters,如果我改變,它的作品,但我需要wrap_parameters在我的應用程序的其他地方..只是不在這個特定的方法。我能做些什麼呢? – 502502

回答

0
class Api::Amazons3Controller < Api::BaseController 
    self.include_root_in_json = false 
end 
+0

這引發一個異常:ActionController :: RoutingError(未定義的方法'Api_ :: Amazons3Controller:類的include_root_in_json =') – 502502

0

試試這個,然後

配置/初始化/ wrap_parameters.rb

if defined?(ActiveRecord) 
    ActiveRecord::Base.include_root_in_json = false 
end 
+0

不,似乎沒有工作 – 502502

0

我能夠通過添加禁用此控制器參數包裝:

class Api::Amazons3Controller < Api::BaseController 
wrap_parameters format: []