views:

110

answers:

2

How would you integrate things like this in a RESTful design?

  • Marking an email as read
  • Voting on a story
  • Reporting a message as inappropriate

Additionally, how would you do it in such a way that one could make a small icon link or button to do the action without too much wizardry?

A: 

This is an indispensable resource for anything REST:

http://oreilly.com/catalog/9780596529260/restful web services

klyde
+4  A: 

That depends on what you're doing. Sometimes all it takes is shifting your viewpoint from "performing an action on a resource" to "creating another, different, but related resource."

Voting on a story is easy: a Vote resource that you can create, review, etc. Same thing with a Report. This can be applied to anything, instead of marking an Order as submitted, it instead would have a Submission, etc. The key is to figure out how to transfigure your verb into a noun.

Marking an email as read is a bit different. What I would do for that is to use a virtual attribute: creating your own setter methods on your model (mark_as_read=, for instance) will let you pass mark_as_read through the params array in an #Update action.

To take the example of voting on a story. What you would do is create a Vote model, which belongs to a Story. Create a Votes controller, and have it nested off the Stories controller in your routes file. then, you can use your nested resource routes (passing in your Story record) to easily create votes scoped to the individual story.

Note that the resource doesn't necessarily have to be backed by a database model, although it would be proper form. You could always simply modify the Story record, in this case, while maintaining the RESTfulness and the ability to easily expand to a full model if needed.

Christopher Swasey
Thanks, this is a very useful answer. But with the 'mark read' one, I've got a bit of a problem. It's a hassle to get a button to pass that parameter when it does the PUT. Any way to make this nicer?
sjmulder
Why can't you just make it a small form with a single hidden field named message[mark_as_read] with value of 1? You can specify the http method in the form tag helper.
Ben