【发布时间】:2017-03-07 07:20:16
【问题描述】:
我正在学习如何使用 RoR 编写一个宁静的 API,并有一个与之相关的问题。所以,我将解释我在代码中做了什么。
这是我的控制器的样子:
class EmployeesController < ApplicationController
require 'rest_client'
def index
uri = "localhost:3000/employees"
rest_resource = RestClient::Resource.new(uri)
users = rest_resource.get # will get back you all the detail in json format, but it will we wraped as string, so we will parse it in the next step.
@users = JSON.parse(users, :symbolize_names => true) # convert the return data into array
@users.each do |u|
logger.info(u.id)
end
# return based on the format type
respond_to do |format|
format.json {render json: @users}
format.xml {render xml: @users}
format.html
end
end
结束
在我的 Gemfile 中,我也包含了 rest-client。
gem 'rest-client'
我的路线是: 根'员工#index' 资源“员工”
希望到目前为止一切都好。
现在,当我发送时:
-> curl请求到'http://localhost:3000/employees',卡住了。
-> 获取请求(通过在浏览器中输入)'http://localhost:3000/',它也卡在这里。
我错过了什么?
【问题讨论】:
-
/employess是否命中了EmployeesController#index? -
@Santhosh,是的,它成功了。
-
这不是无限循环吗?
-
@Santhosh,为什么?
-
你的目标是返回 Json 吗?
标签: ruby-on-rails ruby rest