简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM