2012-06-26 43 views
0

我正在尝试将公共API添加到我的应用程序,以允许用户通过API调用将项目添加到我的应用程序中。Rails - 通过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功能?但我很困惑这整个过程是如何工作的?

例如,如果我的事件模型只有一个字段=>名称:字符串

我将如何能够通过网络服务来添加一条记录?

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

回答

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