1

I need to generate a JSON from a form_tag. The structure I'm trying to generate is something like this:

"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"
    }
]

I'm doing it like this:

<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>

My controller is:

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

I need to add more objects inside this array. How can I do that?

2
  • Please, provide models and controller which renders this view. Commented Jan 14, 2016 at 13:00
  • The only thing I do have is a Controller to get the information from the form and send it to the request. Commented Jan 14, 2016 at 13:05

1 Answer 1

1

For browsers to understand - you only have to duplicate your fields, since you already have array mark in field names, but there seems to be a bug in rack query parser that makes it drop some data on deep nesting:

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"}}]}

While without deep nesting it works:

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"}]}

So as a workaround you can name fields recipients[0][account][accountId], recipients[1][account][accountId] and so on and then recombine the array:

params["recipients"] = params["recipients"].values if params["recipients"].is_a?(Hash)
Sign up to request clarification or add additional context in comments.

1 Comment

Worked just fine! Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.