简体   繁体   English

宁静的API与Rails

[英]Restful api with rails

I am learning how to write a restful API using RoR and have a question related to it. 我正在学习如何使用RoR编写一个宁静的API,并有一个相关的问题。 So, I will explain what I did along with the code. 因此,我将解释我与代码一起做的事情。

This is how my controller looks like: 这是我的控制器的外观:

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

end 结束

In my Gemfile, I have included rest-client as well. 在我的Gemfile中,我还包括了rest-client。

gem 'rest-client'

My routes are : root 'employees#index' resources 'employees' 我的路线是:根'employees#index'资源'employees'

Hope everything is fine till now. 希望到目前为止一切都很好。

Now, when I send: 现在,当我发送:

-> Curl request to ' http://localhost:3000/employees ', it gets stuck. ->将请求卷曲到“ http:// localhost:3000 / employees ”,它被卡住了。

-> Get request(by typing in the browser) to ' http://localhost:3000/ ', it get stuck here as well. ->获取请求(通过在浏览器中键入)到' http:// localhost:3000 / ',它也会卡在这里。

What is that which I am missing? 我想念的是什么?

You don't need RestClient as you're writing a server here, not a client. 在这里编写服务器而不是客户端时,不需要RestClient。 The browser acts as the client. 浏览器充当客户端。 Remove the call to localhost as it's creating a loop. 在创建循环时,请删除对localhost的调用。

The URL for this should already be set in your routes.rb, maybe using: 可能已经在您的routes.rb中设置了此URL,也许可以使用:

 resources :users

Assuming this is a typical app, the show function should be reading from the database using ActiveRecord. 假设这是一个典型的应用程序,则show函数应该使用ActiveRecord从数据库中读取。

class EmployeesController < ApplicationController
  def index
    @users = User.all
    respond_to do |format|
      format.json {render json: @users}
      format.xml  {render xml:  @users}
      format.html
    end
  end
end

Do you have some other application running on localhost:3000? 您是否在localhost:3000上运行其他应用程序? Because if not, then what your server does is calling himself again and again, causing a loop. 因为如果没有,那么服务器所做的就是一次又一次地调用自己,从而导致循环。

If you do have some other application, which fetches users from database, then be sure its running on some other port, other than this your rails app. 如果您确实有其他应用程序可以从数据库中获取用户,则请确保其在其他端口上运行,而不是您的rails应用程序。

If you have only 1 app, then you don't need rest client. 如果只有1个应用程序,则不需要休息客户端。

You actually can do this without any additional gem. 您实际上可以做到这一点而无需任何其他宝石。 You just need to declare your routes according to what you want to expose to your API users and return the type (xml, json, ...) accordingly. 您只需要根据要向API用户公开的内容声明路由,并相应地返回类型(xml,json,...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM