【问题标题】:Getting an Array inside a Rails object generated by a form在表单生成的 Rails 对象中获取数组
【发布时间】:2016-01-14 12:44:56
【问题描述】:

我需要从 form_tag 生成 JSON。我试图生成的结构是这样的:

"recipients": [
    {
        "account": {
            "accountId": "4",
            "account": "4",
            "branch": "4"
        },
        "order": {
            "orderId": "4",
            "dateTime": "4",
            "description": "4"
        },
        "amount": "3",
        "mediatorFee": "0",
        "currency": "0"
    },
    {
        "account": {
            "accountId": "4",
            "account": "4",
            "branch": "4"
        },
        "order": {
            "orderId": "4",
            "dateTime": "4",
            "description": "4"
        },
        "amount": "3",
        "mediatorFee": "0",
        "currency": "0"
    }
]

我是这样做的:

<fieldset>
  <legend>Recipients</legend>
  <p>Account</p>
  <div>
    <%= label_tag 'AccountId' %>
    <%= text_field_tag 'recipients[][account][accountId]', nil , class: "form-control" %>
  </div>
  <div>
    <%= label_tag 'Account' %>
    <%= text_field_tag 'recipients[][account][account]', nil , class: "form-control" %>
  </div>
  <div>
    <%= label_tag 'Branch' %>
    <%= text_field_tag 'recipients[][account][branch]', nil , class: "form-control" %>
  </div>
  <p>Order</p>
  <div>
    <%= label_tag 'OrderId' %>
    <%= text_field_tag 'recipients[][order][orderId]', nil , class: "form-control" %>
  </div>
  <div>
    <%= label_tag 'DateTime' %>
    <%= text_field_tag 'recipients[][order][dateTime]', nil , class: "form-control" %>
  </div>
  <div>
    <%= label_tag 'Description' %>
    <%= text_field_tag 'recipients[][order][description]', nil , class: "form-control" %>
  </div>
  <p>Recipients</p>
  <div>
    <%= label_tag 'Amount' %>
    <%= text_field_tag 'recipients[][amount]', (params['recipients[][amount]'] or 0) , class: "form-control" %>
  </div>
  <div>
    <%= label_tag 'Mediator Fee' %>
    <%= text_field_tag 'recipients[][mediatorFee]', (params['recipients[][mediatorFee]'] or 0) , class: "form-control" %>
  </div>
  <div>
    <%= label_tag 'Currency' %>
    <%= text_field_tag 'recipients[][currency]', (params['recipients[][currency]'] or 0) , class: "form-control" %>
  </div>
</fieldset>

我的控制器是:

require 'rubygems'
require 'httparty'
require 'json'
require 'digest'


class PaymentsController < ApplicationController
 def index
end

  def sendPayment
   params.delete :utf8
   params.delete :commit
   params.delete :controller
   params.delete :action

@jsonParams = params
puts @jsonParams.to_json
@result = HTTParty.post('http://url.url.com'.to_str,
:body => @jsonParams.to_json,
:headers => { 'Content-Type' => 'application/json',
              'Api-Access-Key' => 'xxxxxxxxxx',
              'Transaction-Hash' => 'dsa' } )

 puts @result
#  redirect_to root_path
end

end

我需要在这个数组中添加更多对象。我该怎么做?

【问题讨论】:

  • 请提供呈现此视图的模型和控制器。
  • 我唯一拥有的是一个控制器来从表单中获取信息并将其发送到请求。

标签: ruby-on-rails ruby json rest


【解决方案1】:

为了让浏览器理解 - 你只需要复制你的字段,因为你已经在字段名称中有数组标记,但是机架查询解析器中似乎存在一个错误,使它在深度嵌套时丢弃了一些数据:

q = Rack::Utils.build_nested_query(
   {a:[{b:{c:"this will be lost"}}, {b:{c:2}}]}
) #=> "a[][b][c]=this+will+be+lost&a[][b][c]=2"

Rack::Utils.parse_nested_query(q) # => {"a"=>[{"b"=>{"c"=>"2"}}]}

虽然没有深度嵌套,但它可以工作:

Rack::Utils.parse_nested_query(Rack::Utils.build_nested_query({a:[{b:"will not be lost"}, {b:2}]})) #=> {"a"=>[{"b"=>"will not be lost"}, {"b"=>"2"}]}

因此,作为一种解决方法,您可以将字段命名为 recipients[0][account][accountId]recipients[1][account][accountId] 等,然后重新组合数组:

params["recipients"] = params["recipients"].values if params["recipients"].is_a?(Hash)

【讨论】:

  • 工作得很好!谢谢你:)
猜你喜欢
  • 1970-01-01
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
相关资源
最近更新 更多