Ruby on Rails Rest-Client Gem发送数组参数错误

时间:2018-09-24 15:33:24

标签: ruby-on-rails ruby rest lumen

我们有一个可以捕获X数量输入的表单,因此我们使用数组来设置信息。

当我们打印参数时,看起来像:

{
  "utf8" => "✓", "authenticity_token" => "DfpQDLOJBuc7ViR3XfY3iaK3/E9Hy9uQBWF8fwSkig0nchqj62DptkGunHOrH5bd79J0L+kG0P758cDqLJx7PQ==", "user" => "1", "Base" => {
    "cliente" => "1", "retencion" => "14", "anio" => "2018", "mes_inicio" => "9", "mes_fin" => "9", "serie" => "1", "nacionalidad" => "Nacional", "montoTotOperacion" => "11111.1", "montoTotGrav" => "11111.1", "montoTotExent" => "0.0", "montoTotRet" => "1111.1"
  }, "Dividendos" => [{
    "CveTipDivOUtil" => "01",
    "MontISRAcredRetMexico" => "0.0",
    "MontISRAcredRetExtranjero" => "0.0",
    "MontRetExtDivExt" => "0.0",
    "TipoSocDistrDiv" => "Sociedad Nacional",
    "MontISRAcredNal" => "0.0",
    "MontDivAcumNal" => "0.0",
    "MontDivAcumExt" => "0.0"
  }]
}

如您所见,对象“ Dividendos”可以是多个数组,因此我们将数据发送到我们的API(Laravel流明)。

    response = RestClient.post ENV["URL_API"]+'/createCFDI', { user: current_user.id, Dividendos: params[:Dividendos] }, {content_type: :json, accept: :json}

主要问题是Rails会像字符串一样发送“ Dividendos”的响应数组。

array(2) {
  ["user"]=>
  string(1) "1"
  ["Dividendos"]=>
  array(1) {
    [0]=>
    string(235) "{"CveTipDivOUtil"=>"01", "MontISRAcredRetMexico"=>"0.0", "MontISRAcredRetExtranjero"=>"0.0", "MontRetExtDivExt"=>"0.0", "TipoSocDistrDiv"=>"Sociedad Nacional", "MontISRAcredNal"=>"0.0", "MontDivAcumNal"=>"0.0", "MontDivAcumExt"=>"0.0"}"
  }
}

为什么所有数据都可以,但是仅在嵌套数组上像字符串一样发送?

我们错过了什么吗?

1 个答案:

答案 0 :(得分:1)

问题是params[:Dividendos]返回一个字符串。

您应该将其解析为哈希,然后RestClient会将其再次解析为JSON

Dividendos: JSON.parse(params[:Dividendos])