Ancinek Ancinek - 1 year ago 135
Javascript Question

Rails 5 - sending data from controller to javascript in json format

I want to be able to send an array of object to my Google Maps, so I can display the locations.

What I want to ask is what is the best way to do it?
I was trying to use RABL gem but the tutorials they have are not exactly up to date.

Then I found info about building and API using rails. This seems like a good plan, but I have no idea how to use it. Google is filled with tuts but only for building API-only app, and I already have my app and just want to add an API to it.

Where should I start looking for it?

Answer Source

try this, in your controller:

def something_controller
   @something = YourModel.all
   respond_to do |format|
      format.json { render :json => @something } 

in your javascript if you are using jQuery:

$.getJSON('your_controller/your_action.json', function(data) {
  // and here you can do something with tour json data
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download