Forum: Ruby REST vs Parameters (Rails)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E83a32d0cd5c03099421dcb75926efce?d=identicon&s=25 Chris Davies (Guest)
on 2009-03-20 13:05
(Received via mailing list)
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
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-03-20 13:16
(Received via mailing list)
2009/3/20 Chris Davies <chris-usenet@roaima.co.uk>

> 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!
998dcd37297540b08b20a6558c40ec64?d=identicon&s=25 Zouplaz (Guest)
on 2009-03-20 15:36
(Received via mailing list)
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
E83a32d0cd5c03099421dcb75926efce?d=identicon&s=25 Chris Davies (Guest)
on 2009-03-20 19:05
(Received via mailing list)
Zouplaz <user@domain.invalid> wrote:
> 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
This topic is locked and can not be replied to.