# Implementation of rest in ROR

**URL:** <https://www.ruby-forum.com/t/implementation-of-rest-in-ror/122285>\
**Category:** Rails\
**Created:** [December 6, 2007, 7:15am UTC](https://www.ruby-forum.com/t/implementation-of-rest-in-ror/122285 "2007-12-06T07:15:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 7:15am UTC](https://www.ruby-forum.com/t/implementation-of-rest-in-ror/122285/1 "2007-12-06T07:15:53Z")

</div>

Hi all,

I am new to ROR world. I know the REST conceptually but find the  
difficulties in implementation can anybody guide me that which is the  
best book which i have to read. also some good links of implementation  
of rest in ROR.

If anybody made a small application to restful in rails please forward  
me that application.

i found some demo but i didn’t understand for what purpose they use  
format.xml

def update  
@forum.update\_attributes!(params[:forum])  
respond\_to do |format|  
format.html { redirect\_to @forum }  
format.xml { head 200 }  
end  
end

please help me i want to learn implementation of rest in ROR

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 7:18am UTC](https://www.ruby-forum.com/t/implementation-of-rest-in-ror/122285/2 "2007-12-06T07:18:14Z")

</div>

format.xml is useless unless you want to return an XML file containing  
whatever is being returned in the action. You dont need that line, or  
the  
responds\_to if you’re only going to have one thing it responds\_to.

I don’t know why format.xml is included and if someone could inform us  
why,  
that would be great 🙂

On Dec 6, 2007 4:45 PM, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> please help me i want to learn implementation of rest in ROR
> 
> >

–  
Ryan B.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2007, 8:50am UTC](https://www.ruby-forum.com/t/implementation-of-rest-in-ror/122285/3 "2007-12-06T08:50:08Z")

</div>

Here is the book to start with:

> **[Pragmatic Bookshelf: By Developers, For Developers](https://pragprog.com)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

This has a section on REST and Rails that will explain things to you.

I would also highly recommend the Peepcode screencast on RESTful  
Rails:  
[http://peepcode.com/](http://peepcode.com/)

This line:  
format.xml { head 200 } or can also be written format.xml { head :ok }  
is used to inform a non-browser request (say from curl) that the PUT  
to the existing forum resource was successful. It does not need to  
return the XML of the object because the client already know what that  
is, because it just PUT it to the service. So a HEAD response saying  
OK (200) is all that is required.

Ryan,

> format.xml is useless unless you want to return an XML file containing  
> whatever is being returned in the action. You dont need that line, or the  
> responds\_to if you’re only going to have one thing it responds\_to.  
> Well yes, format.xml is useless unless you want to return XML for the  
> action. But, as it turns out, returning XML for actions is VERY  
> useful.

> I don’t know why format.xml is included and if someone could inform us why,  
> that would be great 🙂  
> You ask why, I ask why not? It is becoming more and more common for  
> web applications to talk to each other. This is generally accomplished  
> using XML to exchange data between applications. Having the scaffold  
> generators include support for XML responses to RESTful actions is  
> extremely useful. It allows the developer to create their REST API  
> right along side their normal site development. Having this “baked-in”  
> support for REST services goes a long way toward a standard where  
> developers can begin to assume other applications support services in  
> a conventional way.

The basic idea here is that it’s time to stop assuming the requester  
is a web page (browser). There are a number of other ways to issues  
requests to a web application in this modern Web 2.0 era. HTML is  
certainly not the preferred response format for, say, terminal request  
via curl. In something like that you don’t want to sift through a  
bunch of HTML layout code to get to your data. XML is a great way to  
supply the data without the presentation. JSON is another great way to  
do that.

Example:

def show  
@forum = Forum.find(params[:id])

respond\_to do |fomat|  
format.html # use the normal erb template to generate the HTML  
response  
format.iphone # use the erb template with optimized presentation  
for the iPhone  
format.xml { render :xml =\> @forum.to\_xml }  
format.json { render :json =\> @forum.to\_json }  
end  
end

curl [http://localhost:3000/forums/1.xml](http://localhost:3000/forums/1.xml) =\> XML response  
curl [http://localhost:3000/forums/1.json](http://localhost:3000/forums/1.json) =\> JSON response
