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
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
returns xml for student(s) with id 42. a post of xml to
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
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
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
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
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
^ ^ ^^^^^^^
^ ^ ^^^^^^^
^ ^ ^^^^^^^
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