From: James Coglan Date: 2009-03-20T21:12:23+09:00 Subject: Re: REST vs Parameters (Rails) --001485f7786c1766b404658bde17 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 2009/3/20 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? 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! -- James Coglan http://github.com/jcoglan --001485f7786c1766b404658bde17--