Can we talk about REST?
It’s been about two years since the Simply RESTful plugin was added to Rails edge core, in that time I think I’ve learned the following two things:
- REST is a very elegant way to structure an external web-service interface to a web application.
- REST is kind of an awkward way to manage the internal structure of an Ajax-heavy web application.
I think you are going to have one of three responses. I’m betting on the middle one:
- “Everybody knows that“
- “You are a loser who has obviously not grasped the zen essence of REST.”
- “You have totally changed my way of thinking”
So here’s where I get into RESTful trouble…
I’ve got a page. It’s basically an index list, but it’s got all this Ajaxy stuff. It’s got a show/hide for the new form, an autocomplete for search, a show/hide for advanced search, a “show details” button that triggers an overlay, you can sort the columns. The advanced search is the flexible one I mention here, so it’s got several Ajax calls associated with it as well.
My first question is where do I put all these methods? Okay, some of them have pretty clear RESTful places. The show new form could be
new.js, all the sort columns can be
index.js, I get that.
But I still have things that don’t seem to fit. Where does the hide form action go? The various actions triggered by parts of the advanced search form? I’m genuinely not sure where a strict RESTful design would have me put them.
Unless I’m missing something, a strict RESTful design would have me do one of two things. (Again with the numbered lists…)
- Shoehorn the functionality into one of the standard RESTful actions, presumably with parameters to govern the actual behavior. This works very well for some things, like the sorting behavior of the actual index. In the general case, it can obviously be overdone — you could wind up with a large and unwieldy controller action.
- Add a custom action in the routes file and add a separate action. Again, this sort of works well for some things (although I find the API for adding custom routes to be unusually opaque for Rails). I have had some weirdness with variant HTTP actions and Ajax calls, especially since
link_to_remotedefaults to a POST and REST anything that you would conceive as show-like to be a GET, but I can usually work around that. Still, I find this path to be awkward, and again, it’s not clear to me exactly what REST is buying me in this case.
After banging my head against this particular wall for a few times, I tried something a little weird on one recent project. There were several controllers that had similar behavior to what I just described. In this case, I put most of the view logic in a helper object that each individual controller would create and render.
The Ajax content I put in a separate Ajax-only controller, which was not RESTful. This let me separate the user and service facing RESTful interface from the Ajax stuff used only by the site itself — it also made it easy for all the different controllers to get common Ajax behavior.
It’s worked tolerably well, although there’s the clear potential to wind up with a controller full of random junk — a controller equivalent of ApplicationHelper, if you will.
Enough about me. How have you solved similar problems?