- 浏览: 94756 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
clcai:
[/color][color=green]
div实现模态窗口(转) -
clcai:
[align=left][/align][size=small ...
div实现模态窗口(转) -
reyesyang:
很有帮助的文章。
[翻译]来自Rails Envy的Rails Cache教程 part1 -
JasonChi:
Hooopo 写道google group的ROR北京社区在哪 ...
rails 学习(转) -
Hooopo:
google group的ROR北京社区在哪里啊
rails 学习(转)
<!-- .entry-meta -->
Rails enables web services by default, which is pretty awesome, and I’ve been relying on that for a while. It is pretty nifty how Rails will magically parse XML post parameters, create an in-memory object and then save that object to the database without your having to write one line of code. However, when the magic fails it can be pretty hard to debug. I found it useful to run basic tests on the command line using curl (hearing the voice of Zach Moazeni in my head saying: “test your assumptions.”)
Below is a writeup of the set of curl commands and sample output for testing the default Rails XML REST APIs. This can serve as a cheat sheet for the experienced or an introduction for folks new to rails who want a basic understanding of the default webservice APIs.
Create an app, by typing the following commands into your terminal:
$ rails basic_app
$ cd basic_app
$ ./script/generate scaffold project title:string description:text
$ rake db:migrate
$ ./script/server
In Rails 2.3, you also need to added the following line to the top of app/controllers/projects_controller.rb (This will allow external access to the APIs.) You can make this change while the server is running, btw.
skip_before_filter :verify_authenticity_token
Leave that window open where you can see it, since it will output useful stuff from the log. Then in another terminal window, experiment with the following commands to interact with your application APIs.
Create
POST /projects.xml
Create a project object based on the XML representation given in the post body and save in the projects database table.
$ curl -X POST -d "<project><title>Awesome</title><description>This is an awesome project.</description></project>" -H "Content-Type: application/xml" http://localhost:3000/projects.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<created-at type="datetime">2009-06-21T10:13:43-07:00</created-at>
<description>This is an awesome project.</description>
<id type="integer">6</id>
<title>Awesome</title>
<updated-at type="datetime">2009-06-21T10:13:43-07:00</updated-at>
</project>
Index
GET /projects.xml
This returns a list of all of the projects in the database with an automatically generated XML representation.
$ curl http://localhost:3000/projects.xml<?xml version="1.0" encoding="UTF-8"?>
<projects type="array">
<project>
<created-at type="datetime">2009-06-21T10:13:19-07:00</created-at>
<description>This is an awesome project.</description>
<id type="integer">1</id>
<title>Awesome</title>
<updated-at type="datetime">2009-06-21T10:13:19-07:00</updated-at>
</project>
<project>
<created-at type="datetime">2009-06-21T10:13:43-07:00</created-at>
<description>New information here</description>
<id type="integer">2</id>
<title>Awesome</title>
<updated-at type="datetime">2009-06-21T10:49:21-07:00</updated-at>
</project>
</projects>
Show
GET /projects/1.xml
This returns an xml representation of the project with id #1
$ curl http://localhost:3000/projects/1.xml<?xml version="1.0" encoding="UTF-8"?>
<project>
<created-at type="datetime">2009-06-21T10:45:19-07:00</created-at>
<description>This is an awesome project.</description>
<id type="integer">8</id>
<title>Awesome</title>
<updated-at type="datetime">2009-06-21T10:45:19-07:00</updated-at>
</project>
Update
PUT /projects/1.xml
This modifies the project with id #1
curl -X PUT -d "<project><description>New information here</description></project>" -H "Content-Type: application/xml" http://localhost:3000/projects/1.xml
----------------------------------------------------------------------
If you’re anything like me, you’ve used cURL to download a batch of MP3 files from the web, or to move a TAR file from one remote server to another. It might come as a surprise, then, that cURL is a full-featured HTTP client, which makes it perfect for interacting with RESTful web services like the ones encouraged by Rails 2. To illustrate, let’s create a small Rails app called ‘tv_show’:
rails tv_show
cd tv_show
script/generate scaffold character name:string action:string
rake db:migrate
script/server
Fire up your web browser and create a few characters. Once you’ve done that, open a new terminal window and try the following:
curl http://localhost:3000/characters.xml
You’ll get a nice XML representation of your characters:
<?xml version"1.0" encoding="UTF-8"?>
<characters type="array">
<character>
<id type="integer">1</id>
<name>George Sr.</name>
<action>goes to jail</action>
<created-at type="datetime">2008-03-28T11:01:57-04:00</created-at>
<updated-at type="datetime">2008-03-28T11:01:57-04:00</updated-at>
</character>
<character>
<id type="integer">2</id>
<name>Gob</name>
<action>rides a Segway</action>
<created-at type="datetime">2008-03-28T11:02:07-04:00</created-at>
<updated-at type="datetime">2008-03-28T11:02:12-04:00</updated-at>
</character>
<character>
<id type="integer">3</id>
<name>Tobias</name>
<action>wears cutoffs</action>
<created-at type="datetime">2008-03-28T11:02:20-04:00</created-at>
<updated-at type="datetime">2008-03-28T11:02:20-04:00</updated-at>
</character>
</characters>
You can retrieve the representation of a specific character by specifying his ID in the URL:
dce@roflcopter ~ > curl http://localhost:3000/characters/1.xml
<?xml version="1.0" encoding="UTF-8"?>
<character>
<id type="integer">1</id>
<name>George Sr.</name>
<action>goes to jail</action>
<created-at type="datetime">2008-03-28T11:01:57-04:00</created-at>
<updated-at type="datetime">2008-03-28T11:01:57-04:00</updated-at>
</character>
To create a new character, issue a POST request, use the -X flag to specify the action, and the -d flag to define the request body:
curl -X POST -d "character[name]=Lindsay&character[action]=does+nothing" http://localhost:3000/characters.xml
Here’s where things get interesting: unlike most web browsers, which only support GET and POST, cURL supports the complete set of HTTP actions. If we want to update one of our existing characters, we can issue a PUT request to the URL of that character’s representation, like so:
curl -X PUT -d "character[action]=works+at+clothing+store" http://localhost:3000/characters/4.xml
If we want to delete a character, issue a DELETE request:
curl -X DELETE http://localhost:3000/characters/1.xml
For some more sophisticated uses of REST and Rails, check out rest-client and ActiveResource.
发表评论
-
Can't install RMagick 2.13.1. Can't find MagickWand.h.
2012-01-09 14:45 2216=== ln -s /usr/local/include/ ... -
rails 3.1 mysql2.gem 安装
2011-11-11 15:20 815http://www.cnblogs.com/ilazysof ... -
用imagemagick和tesseract-ocr破解简单验证码 自我补充记录
2011-10-13 16:28 1958http://hooopo.iteye.com/blog/99 ... -
mac虚拟机本地通过git上传项目到github时出现的诡异问题
2011-10-02 23:27 2016错误为: Permission denied (public ... -
mac rails3.1 启动mysql报错解决
2011-09-27 23:18 1244/Library/Ruby/Gems/1.8/gems/m ... -
windows rails+apache+ssl 配置
2011-09-08 19:51 1103apache 中ssl配置详见: http://apps.hi ... -
ruby on rails linux部署环境下定时任务解决小记
2011-08-31 18:09 1037原本系统在开发环境下的定时任务用的是 rufus-schedu ... -
rails rake 用法说明
2011-08-31 11:44 1460原文:Ruby on Rails Rake Tut ... -
郁闷的部署
2011-08-22 16:11 731今天想给跑在windows下的系统用apache+mongre ... -
浅析Ruby on Rails部署方案(转)
2011-08-22 14:37 910前言 两年过去了,随着客户端数量的不断增加、客户端功能的增加 ... -
rails 学习(转)
2011-08-17 11:15 8911. 书籍: rails 圣经: Agile Web De ... -
AJAX file uploads in Rails using attachment_fu and responds_to_parent
2011-08-12 17:32 561http://khamsouk.souvanlasy.com/ ... -
rails的REST特性简记(转)
2011-07-11 14:53 8651.关于REST的URL的详细讨论,参见《RESTful Ra ... -
ubuntu 10.10 rails 安装
2011-07-09 00:13 739http://floger.iteye.com/blog/93 ... -
rails rmagic安装问题 解决
2011-06-14 12:36 1017一开始手动下载imagemagick安装的,结果配置出现一些问 ... -
如何用nginx+passenger部署Rails(转)
2011-06-10 15:28 1742以前一直用apache+passenger ... -
rails + sqlserver 2005 分页 问题记录
2011-06-08 19:20 852都说sqlserver + rails 用着别扭,终于体会到了 ... -
centos 5.5 rails 安装 参考
2011-05-19 17:50 10361.安装gcc yum install gcc-c++ y ... -
centos 5.5 rails 安装 参考
2011-05-19 16:40 1129这二天部署一个项目,部署在Centos 5.5 下,本来用的u ... -
rails应用与多数据库的连接
2011-04-15 15:20 917rails与多数据库的连接有插件模式,也有原生模式,本文参考互 ...
相关推荐
Web Services on Rails Web Services on Rails Web Services on Rails
《Agile Web Development with Rails》是一本经典的Rails开发指南,中文版的出版使得更多的中国开发者能够深入理解并应用敏捷开发方法与Ruby on Rails框架。这本书是Rails开发者的必备参考资料,它详细介绍了如何...
Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...
书中的"Pragmatic.Bookshelf.Agile.Web.Development.with.Rails.2nd.Edition.Dec.2006.eBook-BBL"可能是该书籍的电子版文件,它包含了全书的章节和内容。读者可以通过这个电子版深入学习Rails开发的各种技巧和最佳...
《敏捷Web开发与Rails》是一本深度探讨如何利用Ruby on Rails框架进行敏捷Web开发的指导书籍,由Dave Thomas、David Heinemeier Hansson等多位在Rails社区有着深厚贡献的作者共同编写。本书不仅覆盖了Rails的基本...
《敏捷Web开发与Rails:程序指南 第四版》是一本深度探讨使用Ruby on Rails框架进行敏捷Web应用开发的专业书籍。本书旨在帮助开发者充分利用Rails 4的特性,提高开发效率,实现快速迭代和高质量的代码编写。 Rails是...
### 敏捷Web开发与Rails 3:关键知识点解析 #### 一、Rails版本与兼容性 本书《敏捷Web开发与Rails》第三版是基于Rails 2编写的。截至本书印刷时,当前可用的Rails Gem版本为2.1。书中所包含的所有代码均已在该...
agile web development with rails 5(英文电子书).............................................................................................................................................................
Agile Web Development with Rails 1-14节_ppt(老师发的修正版)
《敏捷Web开发与Rails》第四版是一本专为软件开发者设计的权威指南,全面涵盖了使用Ruby on Rails框架进行敏捷Web应用开发的知识。Rails 3是该版本的重点,它引入了许多新特性和改进,使得开发过程更为高效且灵活。...
《Pragmatic - Agile Web Development with Rails》是Ruby on Rails框架的经典教材,旨在引导初学者高效地学习敏捷Web开发。这本书的第三版于2009年发布,它结合了Pragmatic Programmers的实用主义理念与Ruby on ...
### Agile Web Development with Rails for Rails 3.2 #### 核心知识点概览 - **Rails 3.2概述** - **敏捷开发方法论** - **Model-View-Controller (MVC) 模式** - **Ruby on Rails基础与高级特性** - **面向对象...
《Agile Web Development With Ruby On Rails》是两本广受欢迎的书籍,主要涵盖了使用Ruby on Rails框架进行敏捷Web开发的知识。这本书的第1版和第2版分别详细讲解了如何运用敏捷开发方法来构建高效、可扩展且易于...