user1571609
user1571609

Reputation:

Render JSON in Rails

I want to see the following code in json format. When I do so I get a blank page with no content. In the rubymine debugger I have the correct information in json format.

include Java

require 'net/http'

class PatientRecordController < ApplicationController

  def index
    url = 'http://localhost:8080/patient/record/v2/'
    data_raw = Net::HTTP.get_response(URI.parse(url)).body

    @data_json = ActiveSupport::JSON.decode(data_raw)

    respond_to do |format|
      format.json {render json: @data_json}
    end

  end
end

Upvotes: 1

Views: 3149

Answers (3)

Michael Lawrie
Michael Lawrie

Reputation: 1554

If you don't want to change the request to include .json, you can just drop the respond_to block render json: @data_json on its own will work.

Upvotes: 0

Igor Kapkov
Igor Kapkov

Reputation: 3909

You must add .json at the end of the path you enter into the browser.

Upvotes: 3

zeantsoi
zeantsoi

Reputation: 26193

What is the path to this particular index action? This code should indeed render JSON, which your debugger is indicating that it indeed is.

The URL you should be visiting is something akin to http://your_host/patient_records/index.json.

Upvotes: 0

Related Questions