I need to point out that the book that really helped me get my head straight on REST, was "The Rails Way" by Obie Fernandez. His chapter 4 tells it like it is, and is probably the first description that didn't just try to blow smoke up my nose.

My unavoidable rant on REST

Rails has it's own world of jargon. Jargon is unavoidable in the world of technology. What becomes more than annoying is being beat over the head about "best practices" and not adhering to "REST" principles. I don't particularly care about what somebody else thinks are best practices, and I have been writing software long enough that I have no tolerance for someone riding a high horse telling me there is only one way to do things. Show me how your methods help me get the job done and make my software maintainable in the long term and I will jump on board with you in a hurry.

REST stands for Representational State Transfer and is a fancy term that encompasses the stateless design of the HTTP protocol and hence the World Wide Web as a whole.
A short list of REST principles follow:

Now back to rails. On one hand adherence to "RESTful" practices is used as a big stick to beat people over the head, while on the other hand rails embraces non-REST practices. Rails supports sessions, which are commonly implemented via cookies. Cookies are almost the defacto non-REST web practice used world-wide. The ivory tower condemns them, the real world uses them every day. And then we get to the business of using the verbs provided to us by HTTP. The verbs we would like to use are GET, POST, PUT, and DELETE. We are immediately confronted by a problem - only GET and POST are supported by real world browsers! That doggone real world getting in the way of the ivory tower again. So rails, hell bent on following REST "best practices" come what may, puts a hidden field "_method" onto POST requests and sets it to "put" or "delete" as necessary. A dirty little secret the high horse REST crowd would probably be reluctant to admit, if they are aware of it at all.

My suspicion is that the rails REST zealots are just fundamentalist islamics that don't yet realize that they were born in the wrong country. I am, on the other hand, entirely utilitarian about my programming tools. I am not trying to make them into a religion, and I place value on them only according to how well the perform as tools, not their moral purity.

Getting something good out of REST

OK, back to getting things done with rails. The good news is that if you do:
map.resources	:things
And you have a model called "thing", you get seven handy routes set up for you as a package deal. Here are the controller actions and the routes set up to map to them: Also note that there are two pairs of actions new and create go together, as well as edit and update. The first (new or edit) is used to produce a form; the second (create or update) is used to process the form. If we put this into the framework of CRUD, we get: OK, all this makes good sense and is a framework I can live with and understand. Most rails adherents come to view this set of conventions as what being "RESTful" is all about, and feel threatened if you depart from them.

To generate a link to a form that will crank out a new item, do this:

link_to "Create a new thing", new_thing_path
This produces a URL to /things/new, which does GET by default.

To generate a link to a form that will edit an already fetched item, do this:

edit_thing_url(@thing)
The tricky business to insert the _method settings is done for you in the form generation methods provided by rails.

This is what you really need to know about being "RESTful".


Feedback? Questions? Drop me a line!

Ruby on Rails notes / [email protected]