Amarpreet Jethra
Amarpreet Jethra

Reputation: 141

multiple render in rails controller action while calling an api

I want that my rails controller index action to render multiple output at once , my controller:

 class Api::V1::Ola::OlaBookingsController < ApplicationController
 def index
   lat = params[:lat].to_s
   long = params[:long].to_s
   drop_lat = params[:drop_lat].to_s
   drop_lng = params[:drop_lng].to_s    
   ola_query = { 
     "pickup_lat" => lat,
     "pickup_lng" => long,
     "drop_lat" => drop_lat ,
     "drop_lng" => drop_lng
   }
   ola_body = {
     "pickup_lat" => lat,
     "pickup_lng" => long,
     "drop_lat" => drop_lat,
     "drop_lng" => drop_lng,
     "pickup_mode" => "NOW",
     "category" => "auto"
   } 
   ola_headers = {
     "Authorization" => "Bearer ", 
     "X-APP-TOKEN" => ""
   }

   @ola_products = HTTParty.get(
     "http://sandbox-t.olacabs.com/v1/products", 
     :query => ola_query,
     :headers => ola_headers
   ).parsed_response

   @ola_booking = HTTParty.post(
     "http://sandbox-t.olacabs.com/v1/bookings/create ", 
     :body => ola_body,
     :headers => ola_headers
   ).parsed_response

  render :json => @ola_booking 
  render :json => @ola_products 
  end
end

I want both responses to be coming on controller without generating a viw. But it gives error "multiple render not possible" , how to fix it?

Upvotes: 1

Views: 720

Answers (2)

Santosh Sharma
Santosh Sharma

Reputation: 2248

you can try this.

respond_to do |format|
  format.json  { render :json => {:ola_booking => @ola_booking, 
                                  :ola_products => @ola_products }}
end

Upvotes: 0

MZaragoza
MZaragoza

Reputation: 10111

You can not have 2 renders what you can do is combine the 2 objects one after the other like

render :json => @ola_booking.to_json + @ola_products.to_json

you should try it out and let me know how it worked

Upvotes: 2

Related Questions