S_A
S_A

Reputation: 423

How can I return a Json in controller action

I have this function in my controller:

gerencia.pay_charge(params: params, body: payment)

This function returns:

{"code"=>200, "data"=>{"barcode"=>"03399.75039 21000.000006 74992.301015 6 69020000002250", "link"=>"https://exmaple.com.br/emissao/110276_19_NAEMAL1/A4XB-110276-74992-XILE0", "expire_at"=>"2016-08-30", "charge_id"=>97611, "status"=>"waiting", "total"=>2250, "payment"=>"banking_billet"}} 

How can I send it in json format?

I have the example in php but I don't know how do it in rails.

In php is this:

$payment = $apiGN->payCharge($params, $paymentBody);
echo json_encode($payment);

Upvotes: 0

Views: 1112

Answers (1)

Arun Kumar Mohan
Arun Kumar Mohan

Reputation: 11905

Try

render json: gerencia.pay_charge(params: params, body: payment)

The above line of code sets a Content-Type of application/json and automatically converts the object(ruby hash) to JSON.

For more, look for rendering json in http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

Upvotes: 2

Related Questions