# Restful interfaces and preserving PATH\_INFO in custom dispat

**URL:** <https://www.ruby-forum.com/t/restful-interfaces-and-preserving-path-info-in-custom-dispat/52074>\
**Category:** Rails\
**Created:** [December 4, 2005, 8:44pm UTC](https://www.ruby-forum.com/t/restful-interfaces-and-preserving-path-info-in-custom-dispat/52074 "2005-12-04T20:44:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 4, 2005, 8:44pm UTC](https://www.ruby-forum.com/t/restful-interfaces-and-preserving-path-info-in-custom-dispat/52074/1 "2005-12-04T20:44:19Z")

</div>

i’m working on a restful interface to existing models in an app.  
ideally i’d  
like to preserve the existing dispatch mechanism and support a parrallel  
one  
that looks like

[http://0.0.0.0/rest/mime/model/id](http://0.0.0.0/rest/mime/model/id)

to do this i’d like to setup a rest controller which dispatches based on  
the  
http request method and path into. for example, a get sent to

[http://0.0.0.0/rest/xml/student/42](http://0.0.0.0/rest/xml/student/42)

returns xml for student(s) with id 42. a post of xml to

[http://0.0.0.0/rest/xml/student/42](http://0.0.0.0/rest/xml/student/42)

updates the record via a xml doc. of course i’ll never use these and  
will  
prefer

[http://0.0.0.0/rest/yaml/student/42](http://0.0.0.0/rest/yaml/student/42)

😉

because i want to preserve the default rails dispatching mechanism and  
isolate  
all rest code into a single controller, thereby enabling the site to be  
rest  
enables for various formats with the addition of a single controller and  
no  
other code modification. the tricky bit is

- how to configure rails such that the path\_info (/student/42) is  
preserved
- how to reuse as much of the existing route/dispaching code as  
possible to  
handle parsing this path\_info

any hints appreciated.

## -a

# =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | – bodhicaryavatara

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 4, 2005, 11:03pm UTC](https://www.ruby-forum.com/t/restful-interfaces-and-preserving-path-info-in-custom-dispat/52074/2 "2005-12-04T23:03:51Z")

</div>

On 4-dec-2005, at 20:42, Ara.T.Howard wrote:

> based on the  
> prefer  
> controller and no  
> other code modification. the tricky bit is
> 
> - how to configure rails such that the path\_info (/student/42) is  
> preserved
> - how to reuse as much of the existing route/dispaching code as  
> possible to  
> handle parsing this path\_info
> 
> any hints appreciated.

map.connect 'rest/:format/:model/:action/:id

on the controller side

def index  
request.post? ? update\_student : return\_student  
end

I think it should parse the params for you if they are YAML or XML  
but I’m not sure.  
Why would you want path-info here?

–  
Julian ‘Julik’ Tarkhanov  
me at julik.nl

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 4, 2005, 11:48pm UTC](https://www.ruby-forum.com/t/restful-interfaces-and-preserving-path-info-in-custom-dispat/52074/3 "2005-12-04T23:48:32Z")

</div>

On Sun, 4 Dec 2005, Julian ‘Julik’ Tarkhanov wrote:

> > any hints appreciated.
> 
> map.connect 'rest/:format/:model/:action/:id  
> ^  
> ^  
> ^

this is inferred from the REQUEST\_METHOD.

the idea is that a url like

[http://127.0.0.1/rest/student/42](http://127.0.0.1/rest/student/42)

is used for all of PUT (create), GET (read), POST (edit), and DELETE  
(delete).

> on the controller side
> 
> def index  
> request.post? ? update\_student : return\_student  
> end

but a post can be either an insert or an update…

> I think it should parse the params for you if they are YAML or XML but I’m  
> not sure. Why would you want path-info here?

[http://127.0.0.1/rest/xml/student/42](http://127.0.0.1/rest/xml/student/42)  
^ ^ ^^^^^^^  
^ ^ ^^^^^^^  
^ ^ ^^^^^^^

```
         controller action path_info

```

but now i think i understand what you are getting at… maybe a

map.connect ‘rest/:format/:model/:id’

for a specific record

and

map.connect ‘rest/:format/:model/’

for all tuples.

but i’m new to routes and don’t quite get the exact approach.

thanks for the idea.

## -a

# =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | – bodhicaryavatara
