simple web services with 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.

 

CentOS 7 Minimal 下部署 Diaspora 分布式社交平台实战 分布式社交是一种基于开放协议、多节点协同的通信范式,其核心原理在于去中心化身份认证、联邦式数据同步与服务自治。技术价值体现在用户数据主权回归、规避平台单点风险、支持内网离线协作等工程优势。典型应用场景包括企业私有社交网络、科研机构知识共享平台、教育组织安全通讯系统等。本文聚焦于在 CentOS 7 Minimal 这一轻量可控Linux环境中,完成 Diaspora 的全栈部署——涵盖 Ruby 2.4(SCL)、PostgreSQL 9.6 编译安装、Elasticsearch 5.6 内存调优及 sys 阅读详情

相关推荐

Ubuntu + Docker 部署 Gitea:轻量级私有 Git 服务最佳实践

Gitea 是一款基于 Go 开发的轻量级自托管 Git 服务,以低资源占用、SQLite 单文件存储和容器友好性著称;其核心原理是通过进程隔离与声明式配置实现环境一致性,技术价值在于规避传统包管理带来的依赖冲突、权限混乱与升级风险;广泛应用于中小团队代码协作、嵌入式开发版本管理、NAS 私有化部署及 DevOps 流水线集成等场景;本文聚焦 Ubuntu LTS 系统下基于 Docker Compose 的生产级 Gitea 部署方案,深入解析 UID 权限控制、健康检查机制、Nginx 反向代理与 SS

congsikuai0611的博客 383

Rails项目中集成InvoicePrinter的3种最佳实践:快速生成专业PDF发票

在Rails项目中集成InvoicePrinter能够让你快速生成专业的PDF发票,这个简单而强大的Ruby库为开发者提供了完整的发票生成解决方案。无论你是构建电商平台、SaaS应用还是需要发票功能的任何Rails应用,InvoicePrinter都能让你的发票生成变得轻松高效。📄 InvoicePrinter是一个纯Ruby的PDF发票生成库,它不依赖系统库或浏览器,渲染速度快,非常适合在请

gitblog_00538的博客 406

从零开始:用YOLOv8-Pose实现实时人体姿态检测(附完整代码)

本文提供了一份从零开始的完整实战指南,详细介绍了如何使用YOLOv8-Pose构建实时人体姿态检测系统。内容涵盖环境搭建、数据准备、模型训练与调优,以及高性能部署和进阶应用开发,并附有可直接运行的代码示例,帮助开发者快速掌握Pose Estimation技术并将其应用于实际项目。

h6i7j8的博客 627

Ubuntu_Server_配置GitLab完全指南

Ubuntu Server 24.04 配置指南 本文提供Ubuntu Server 24.04的详细配置教程,适用于VMware虚拟机环境。主要内容包括: 系统初始化:更新系统、安装基础工具 Docker安装:使用Ubuntu官方源安装docker.io和docker-compose GitLab部署:通过docker-compose配置GitLab容器 网络配置:设置固定IP地址 磁盘管理:VMware扩容及Ubuntu分区扩展方法 实用命令:包括容器状态检查、日志查看等运维操作 该指南特别强调注意事项

weixin_42687723的博客 238

DPABI安装教程

DPABI是是中科院心理研究所严超赣老师团队开发的一款磁共振脑影像数据分析工具包。基于SPM,在安装DPABI之前需要安装好SPM,才能正常使用。SPM的安装方法见文matlab2021a版本条件下安装spm_kidllll的博客-CSDN博客 1、下载安装包 下载地址:DPABI: a toolbox for Data Processing & Analysis for Brain Imaging | The R-fMRI Network 2、将下载好的dpabi文件夹移动到matl..

qq_42356925的博客 1万+

阿里云学生机2核4G,30分钟搞定DNF台服联机(CentOS 7.6保姆级教程)

本文提供阿里云学生机2核4G搭建DNF台服联机的详细教程,涵盖服务器选购、网络配置、服务端部署及客户端联机设置。通过CentOS 7.6系统优化和端口配置,30分钟内即可完成搭建,适合学生和技术爱好者低成本实现游戏联机体验。

weixin_42537919的博客 375

ruby on rails

您很可能听说过 Ruby on Rails。甚至有可能使用过它;或者是刚刚开始使用它进行编程。相信大家已经看出本文的主题就是 Rails,不论读者属于哪种情况,都将从本文中受益。Ruby 与 XML 是一对梦幻组合 —— 欲知详情请继续阅读。 开始之前 本教程面向那些想要了解关于建立 Rails 应用程序框架及使用 Ruby 和 Rails 处理 XML 的基础知识的开发群体。初级和中级程...

从心开始,重新开始 287

论文笔记:Group Equivariant Convolutional Networks

Group Equivariant Convolutional Networks 1、四个问题 要解决什么问题? 对卷积神经网络进行扩展,并提出一个在特定的变换(旋转、平移等,也可表示为一个特殊的群)下具有等变性的网络。 用了什么方法解决? 提出了一种新的卷积神经网络结构——群等变卷积神经网络(Group equivariant Convolutional Neural Network)...

hongbin_xu的博客 1万+

[翻译]来自Rails Envy的Rails Cache教程 part2

原文地址:http://railsenvy.com/2007/3/20/r ... ing-tutorial-part-2 Part1地址:http://www.ruby-lang.org.cn/forums/thread-3158-1-1.html 本教程的编写顺序是按照各个缓存的效率来排序的,Page缓存最快,所以在第一篇教程就介绍了,这篇教程就介绍其它的几种缓存。 Action 缓存 ...

从心开始,重新开始 276

锁定放大器

锁定放大器

qq_46757048的博客 4720

离线安装vue脚手架,内网安装@vue/cli

公司开发环境使用内网,无法用外网,搭建环境的时候,发现内网系统没有安装vue脚手架,我搜了很多文章,发现他们的方法对我都不适用,然后就自己摸索,最终皇天不负有心人,终于被我安装上了,一下是我内网离线安装vue/cli的方法,希望能帮助到有需要的小伙伴! 因为npm被集成在nodejs中,而安装nodejs只需要把nodejs安装包copy到内网电脑安装就行,比较简单,这里就不多做演示。 在外网电脑上,使用npm install -g xxxx命令下载好我们所需要的各个安装包,比如: npm insta

Liberty_yes的博客 1万+

rails select使用小结

最近经常使用select下拉标签 个人比较喜欢用select 和 select_tag 两个 [code="ruby"] [b]select_tag(name, option_tags = nil, options = {}) [/b] Creates a dropdown selection box, or if the :multiple option is set to tr...

从心开始,重新开始 267

Java小白入门200例81之Java接口

作者简介 作者名:编程界明世隐 简介:CSDN博客专家,从事软件开发多年,精通Java、JavaScript,博主也是从零开始一步步把学习成长、深知学习和积累的重要性,喜欢跟广大ADC一起打野升级,欢迎您关注,期待与您一起学习、成长、起飞! 引言 很多Java初学者问我,新手明明很用心、很努力学习的Java知识,转头又忘记了,很让人犯愁,小白如何能够快速成长、成为大牛呢? 其实要成为大神的技巧只有一个:“多学多练”,于是明哥就整理了比较典型的练习实例,通过练习能够快速提升编码技巧和熟练度,让你在成为大

编程界明世隐的博客 1362

rails 上传文件 form_for与form_tag的区别

用image magic插件实现图片上传, [code="ruby"] from_tag :action=>"create" :multipart=>true do from_for :product,:url=>{:action=>'create'},:html=>{:multipart=>true} do |form| [/code] 为什么form_...

从心开始,重新开始 255

基于ZYNQ的HP总线与DDR交互:实现PL与PS高效数据传输的完整指南

本文将深入探讨如何通过AXI HP总线实现PL与PS的DDR交互,结合代码示例、硬件配置与创新优化,助力开发者掌握这一关键技术。ZYNQ内部通过AXI HP接口直接连接PL与DDR控制器,PL通过AXI协议发送读写请求,绕过PS的CPU直接操作DDR。通过AXI HP总线实现PL与PS的DDR交互,开发者可充分发挥ZYNQ异构架构的优势。在Xilinx ZYNQ系列芯片中,PS(Processing System)与PL(Programmable Logic)的协同设计是其核心优势。

QQ_778132974的博客 1238

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

rails rake 用法说明

原文:Ruby on Rails Rake Tutorial (aka. How rake turned me into an alcoholic) 引言:作为一个rails的开发者,你可能很熟悉使用rake进行你的测试,或者使用rake db:migrate运行你的migrations,但是你真的知道Rake的背后故事吗?你意识到可以自己写一个Rake任务或者一个有用的lib吗? 下面...

从心开始,重新开始 252

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

rails model里面定义get/set方法

[code="ruby"]1. attr_reader :name attr_writer :name [/code] [code="ruby"]2. attr_accessor :name [/code] 1=2

从心开始,重新开始 243

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Can't install RMagick 2.13.1. Can't find MagickWand.h.

=== ln -s /usr/local/include/ImageMagick/wand /usr/local/include/wand ln -s /usr/local/include/ImageMagick/magick /usr/local/include/magick === /usr/local/include/ImageMagick为ImageMagick安装目录...

从心开始,重新开始 232

浅析Ruby on Rails部署方案(转)

前言 两年过去了,随着客户端数量的不断增加、客户端功能的增加、与服务器端交互数据的增加、老板对功能的要求不断增加,我在这个项目上走了不少弯路,尤其是在部署——或者说是架构——方面。 我遇到的最大的问题就在于并发链接数上。服务器与客户端的每次交互的数据量并不大,但内容无法缓存。起初用的是Nginx/Apache+Mongrel的部署方式,但当遇到大量并发请求时,常常会遇到Mongrel进...

从心开始,重新开始 222

如何用nginx+passenger部署Rails(转)

以前一直用apache+passenger部署redmine(一个用 Rails开发的项目管理工具),最近听说一个web服务器后期之秀—-Nginx很是厉害(传说它可以处理的并发链接数是apache的10倍)。于是 今天就上网搜一些关于这方面的资料,以下是安装passenger+nginx,并部署redmine的过程。 一、安装步骤 下载passenger源码包并解压 下载地址:ht...

从心开始,重新开始 218

rails应用与多数据库的连接

rails与多数据库的连接有插件模式,也有原生模式,本文参考互联网资料,总结出来,并实际连接成功 1、配置database.yml development: adapter: mysql encoding: utf8 database: myapp username: root password: host: localhost te...

从心开始,重新开始 211

用imagemagick和tesseract-ocr破解简单验证码 自我补充记录

http://hooopo.iteye.com/blog/993538 用imagemagick和tesseract-ocr破解简单验证码 补充 不知道怎么回事,我这的机器 直接 sudo apt-get install tesseract 不行 下载gz包安装也没成功 结果是 sudo apt-get install tesseract-ocr 还有安...

从心开始,重新开始 208

处理一对多 多端修改的问题

[code="code"] def mpart_atts= atts atts.each do |att| if !att[:part].blank? and att[:id].blank? mparts.build(att) elsif !att[:part].blank? and !att[:id].blank? it...

从心开始,重新开始 205

rails rmagic安装问题 解决

一开始手动下载imagemagick安装的,结果配置出现一些问题混乱死了,导致装上了rmagick还是会报 rmagick文件找不到。 于是安装网上例子 走步骤 sudo apt-get install imagemagick libmagickcore-dev gem install rmagick 结果就出现如下问题: ##This installation of RMag...

从心开始,重新开始 202

mac rails3.1 启动mysql报错解决

[code="java"] /Library/Ruby/Gems/1.8/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dy...

从心开始,重新开始 181

rails 学习

http://www.asciicasts.com/episodes/all

从心开始,重新开始 176
上一篇: rails 小错误引起的 纠结问题(rjs文件罢gong)
下一篇: memcache
findingwork
博客等级 码龄17年 1粉丝 72原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址