company-logo

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:

  1. REST is a very elegant way to structure an external web-service interface to a web application.
  2. 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:

  1. “Everybody knows that
  2. “You are a loser who has obviously not grasped the zen essence of REST.”
  3. “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…)

  1. 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.
  2. 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_remote defaults 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?

  1. Adam Reply

    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.

  2. Jonny Reply

    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.

  3. Seban Reply

    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.

  4. Keenan Brock Reply

    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

  5. masone Reply

    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

  6. Pingback: Pathfinder Development » A Look Back At Past Post

  7. Eric Reply

    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.

Leave a Reply

*

captcha *