简体   繁体   中英

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

您必须在输入浏览器的路径末尾添加.json

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 .

如果你不想改变包含render json: @data_json的请求,你可以放弃respond_to块render json: @data_json本身就可以了。

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