REST vs Parameters (Rails)

C

Chris Davies

I'm still trying to get my head around REST based URIs vs slightly more
conventional cgi script with ?parameters. I think I'm probably making
heavier weather of it than I need to, but I was hoping one of you could
help me out with some understanding...

Consider a situation where I need to generate some reports from a database
(HTML, XML, whatever; it doesn't matter).

Conventionally I might have created a script called report.cgi that took
a number of parameters: { datefrom, dateto, category }, so that I could
obtain a report of, say, sales results across a date range:

report.cgi?datefrom=20090101;dateto=20090320;category=sales


How could/should I represent a URI for this kind of query using the
Rails paradigm?

Many thanks from a confused Rails learner!
Chris
 
J

James Coglan

[Note: parts of this message were removed to make it a legal post.]

2009/3/20 Chris Davies said:
I'm still trying to get my head around REST based URIs vs slightly more
conventional cgi script with ?parameters. I think I'm probably making
heavier weather of it than I need to, but I was hoping one of you could
help me out with some understanding...

Consider a situation where I need to generate some reports from a database
(HTML, XML, whatever; it doesn't matter).

Conventionally I might have created a script called report.cgi that took
a number of parameters: { datefrom, dateto, category }, so that I could
obtain a report of, say, sales results across a date range:

report.cgi?datefrom=20090101;dateto=20090320;category=sales


How could/should I represent a URI for this kind of query using the
Rails paradigm?



As is my understanding (at least, I find it useful to think this way), REST
is about viewing URLs as pointers to resources, and HTTP methods as
different ways of interacting with a resource -- GET for retrieval, POST for
updating, etc. Pretty URLs are orthogonal to REST, thought RESTful thinking
often leads to a hierarchically organised site that lends itself to pretty
URLs.

In your case, the parameters do not lend themselves to hierarchical
organisation entirely. The closest I'd go is something like this (for an xml
report):

/reports/sales.xml?from=20090101&to=20090320

Which, by default, would map to the #sales method in ReportsController, with
params = {:format => "xml", :from => "20090101", :to => "20090320"}. Rails
will handle the format for you so if you wanted html and xml output, create
templates at views/reports/sales.html.erb and
views/reports/sales.xml.builder (assuming you're using ERB and XmlBuilder
respectively).

Hope that helps!
 
Z

Zouplaz

le 20/03/2009 12:34, Chris Davies nous a dit:
report.cgi?datefrom=20090101;dateto=20090320;category=sales


How could/should I represent a URI for this kind of query using the
Rails paradigm?

My own understanding of REST concepts is : be CRUD. And being CRUD don't
prevent you from using parameters.

So, viewing sales report is a show action
/reports/sales?datefrom=20090101;dateto=20090320

In that case, sales is the :id (overiding usual rails integer ID)

Being CRUD, you'll end with more controllers with less actions and
theses actions will most of the time be the classical 7 : index, show,
new, create, edit, update, delete
 
C

Chris Davies

Zouplaz said:
My own understanding of REST concepts is : be CRUD. And being CRUD don't
prevent you from using parameters.

That's very useful to know, as it doesn't seem to be described in any of
the tutorials/books I've read.

I'm of the opinion that not all datasets can be usefully described
in a hierarchical fashion, and trying to force a dataset into a URI
shaped format doesn't make sense to me. On the other hand, I'm open to
education. Always.

Appreciated.
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top