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?