Ted*_*ddy 4 android ruby-on-rails
我正在尝试创建一个应用程序,它基本上让Android设备上的用户发送一个立即出现在rails应用程序中的消息.有没有人建议我会用它做什么?
lbz*_*lbz 13
这是一个快速的演练.
让我们创建一个新的rails应用程序
$ rails new simple-message
$ cd simple-message/
Run Code Online (Sandbox Code Playgroud)
我们现在要创建一个名为Message的RESTful资源,它将管理来自您手机的消息.
$ rails generate scaffold Message title:string content:text
Run Code Online (Sandbox Code Playgroud)
创建将包含消息的表:
$ rake db:migrate
Run Code Online (Sandbox Code Playgroud)
启动绑定服务器并将浏览器指向http://0.0.0.0:3000/messages
从那里,您可以手动创建新消息,这些消息将显示为列表.
对于您可以使用浏览器导航的每个页面,都有一个相应的视图,可以接受来自其他客户端的编程调用.
如果您浏览http://0.0.0.0:3000/messages.xml,您将获得一个包含所有消息的xml.随着卷曲:
$ curl http://localhost:3000/messages.xml
<?xml version="1.0" encoding="UTF-8"?>
<messages type="array">
<message>
<created-at type="datetime">2010-11-27T18:24:36Z</created-at>
<title>Just a message</title>
<updated-at type="datetime">2010-11-27T18:24:36Z</updated-at>
<id type="integer">1</id>
<content>With some content</content>
</message>
</messages>
Run Code Online (Sandbox Code Playgroud)
是时候添加一条新消息了:
$ curl -X POST -H 'Content-type: text/xml' -d '<message><title>Hello</title><content>How are you</content></message>' http://0.0.0.0:3000/messages.xml
<?xml version="1.0" encoding="UTF-8"?>
<message>
<created-at type="datetime">2010-11-27T18:40:44Z</created-at>
<title>Hello</title>
<updated-at type="datetime">2010-11-27T18:40:44Z</updated-at>
<id type="integer">2</id>
<content>How are you</content>
</message>
Run Code Online (Sandbox Code Playgroud)
您的Android客户端必须充当curl才能添加新消息.
现在,您需要进行一些身份验证,以便仅允许某些用户充当管理员并限制使用您的API.在挖掘Rails中实现身份验证的各种方法之前:
您可以在这里找到不同策略的精彩回顾.
| 归档时间: |
|
| 查看次数: |
2757 次 |
| 最近记录: |