- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
I’m Cranky Because I’m Not Getting Enough REST
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?
Topics: Ruby on Rails
Comments: 7 so far
Leave a comment
About Pathfinder
Recent
- Firefox Plugin Malware ‘Trojan.PWS.ChromeInject.A’
- Pathfinder releases version 1 of the its Flash Platform microsite (codename Mica)
- Pimp my Rails: Five Plugins & Gems to Make Rails Better
- iPhone: Using Pre-processor Directives for Device Testing
- Subtle OpenGL Projection Matrix Difference Between iPhone Simulator and Device
- App Security: Throw Out the Org Chart!
- Pimp my jQuery: Five plugins to replace the features Prototype and Scriptaculous users expect
- Thanksgiving 2008: What We’re Thankful For (In Rails)
- iPhone SDK: Testing with TextMate & GTM
- GWTQuery - JQuery-like Syntax in GWT
Archives
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006



I don’t have any real earth shattering revelation but I’ve always thought of it as dMVC (distributed MVC). That is, treat your RESTful data source(s) as nothing more than a model. Your HTML & most of the JavaScript is your View. The AJAX library is the controller (and there is some JavaScript that handles something like routing).
I’m not sure how that helps, but that’s how I do it.
Comment by Adam, Friday, August 22, 2008 @ 4:16 pm
What are your ajaxy things accessing when they make a call to the server? The way that I understand it, it would be fine to put all of those things on a page template without being unrestful. The point would be that you make them access the right resource when they do something on the server. Basically when you’re rendering your “index with a bunch of other stuff” page, you would consider those ajaxy things as just random html tags. Put them into a helper if the same thing will show up in a lot of different places. The restful part comes in when they actually do the ajaxy stuff, not when they are rendered.
Maybe it helps to think of a search box as just a more complicated link. If I’m on controller foo/index and I put a link to bar/index, that’s not being unrestful by any stretch of the imagination.
Does that make sense? I’m new to rest, so I’m still trying to think through a lot of these issues too.
Comment by Jonny, Friday, August 22, 2008 @ 7:11 pm
I write my first RESTful project right now, but I don’t have problem descibed by you. I think this is not bad what you do. Probobly I will do the same.
Comment by Seban, Saturday, August 23, 2008 @ 3:59 am
You touched on a key point here. Rest is about accessibg resources. It isn’t about a granular client.
A word processor needs a document service to crud a document. But the front end does more stuff like spell checking. That is a client task not a document service thing.
So when you have a webapp and the client functionality is running on the server, things get confusing.
So separating the document restful service from the application/client/wordprocessor sounds great.
For some reason I was expecting to see flames though
Comment by Keenan Brock, Monday, August 25, 2008 @ 6:18 pm
I also think that Javascripts currently have no defined place in Rails. I recently was kind of annoyed by that fact. Why can’t we handle js.erb templates like we are using html templates?
My solution was a quick plugin which automatically includes the template in the file controller/action.js.erb (if existing). It’s nice because everything has a defined place, you can parse erb in your Javascript files and content and Javascript code are separated.
This does not solve all problems but maybe it helps you too?
http://github.com/masone/js.erb-auto-include-plugin/tree/master
Comment by masone, Tuesday, August 26, 2008 @ 2:06 am
[...] REST [...]
Pingback by Pathfinder Development » A Look Back At Past Post, Friday, October 3, 2008 @ 11:10 am
I think ultimately what bother you here is something not actually “resource”, like the “hide” action. It does not make sense to make them resourceful.
Comment by Eric, Tuesday, November 25, 2008 @ 4:06 am