Search code examples
ruby-on-railsjsonrubyrails-console

How to print JSON object in browser console, using ruby controller?


I want to print a JSON object in the browser's console, I searched a lot but I din't find any appropriate result.

Here is my ruby controller:

class ScheduleTime < ApplicationController

available_schedule = ScheduleTimeSlot.where(:doctor_id => params[:dr_id],:date=>params[:for_date].to_date,:status=>"vacant",:archive=>false).reorder(:start_time)

render :json =>{:available_schedules=>available_schedules}

//here I need a syntax that print the available_schedule on browser console (if possible) or ruby console, so I can debug it properly.

end

Thanks In Advance


Solution

  • You can't access the browser's console directly from your controller, and if you're trying to do so, it's probably because you're doing something wrong. Your controller (like all Ruby code in your application) runs on the server, and whatever logging you do will be output in the server's logs, not on the client.

    If you want to write to the browser's console, you need to send JavaScript to the client which does that. That means outputting script from your controller to the browser. You will have to wrap your JSON output in <script>console.log(...)</script> and change your output format to JavaScript, not JSON.