Rails - 通过API向应用添加/发布新记录

时间:2012-06-26 17:41:32

标签: ruby-on-rails ruby-on-rails-3 api

我正在尝试向我的应用添加公共API,以允许用户通过API调用将项添加到我的应用中。

虽然我有点困惑.....

到目前为止,我已经输出了一个很棒的xml页面,其中所有的模型数据都非常完美......

def index   
  @events = Event.all
  respond_to do |format|
    format.html 
    format.xml  { render :xml => @events }
    format.json { render :json => @events }
  end
end

所以我假设我能够将相同的respond_to块添加到CREATE或NEW操作(哪一个?)并在那里获得某种形式的API功能?但我对整个过程的运作方式感到困惑......

例如,如果我的事件模型只有一个字段=> name:string

我如何通过网络服务添加记录?

  ???? ==> curl http://localhost:3000/events[??????add??????]

1 个答案:

答案 0 :(得分:1)

See this post使用cURL测试REST应用程序。

引用:

-X [action]: Allows you to specify an HTTP action such as GET, POST, PUT or DELETE.
Example:

curl -X DELETE http://localhost:3000/books/1

-d [parameter]: Lets you set variables as if they were POSTed in a form to the URL. Note that this automatically makes the request a POST HTTP action type (no -X necessary).
Example:

curl -d "book[title]=Test" -d "book[copyright]=1998"
http://localhost:3000/books

-H [header]: Gives you the option of setting an HTTP header such as Content-Type or Accept. This is particularly useful for requesting text/xml as the Accept type.
Example:

curl -H "Accept: text/xml"
http://localhost:3000/books/sections/1