在Rails中渲染JSON

时间:2013-06-10 18:31:24

标签: ruby-on-rails ruby json jruby

我希望以json格式看到以下代码。当我这样做时,我得到一个没有内容的空白页面。在rubymine调试器中,我有正确的json格式信息。

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

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

此特定index操作的路径是什么?这段代码确实应该呈现JSON,你的调试器指的是它确实是。

您应该访问的网址类似于http://your_host/patient_records/index.json

答案 2 :(得分:0)

如果您不想更改包含.json的请求,您只需删除respond_to块render json: @data_json就可以了。