Topic: What's the real advantage of REST ?

Hello,

I wrote several smaller applications with Rails and I still don't get the real thing about RESTful design.

CRUD operations that work on single model objects are relatively sparse in my applications. Most of the time there's fine-grained user interaction (like 'activate_user_account' instead of 'edit_user'), or I have pages that manipulate more than one objects in a single form. As soon as there's Ajax in the game RESTful design doesn't fit at all.

Moreover manipulating object trees (in comparison to single objects) with RESTful design on one page is complicated if not a design-contradiction at all.

The multi-format output (i.e. getting XML from the same URL) is nice, but not really relevant for all but very few web applications.

Having to maintain routes explicitly removes some of Rails' advantages regarding agile development.

And furthermore, the current generating scripts support RESTful design only under very general circumstances (generating a model object with everything in one step, but to my knowledge I can't generate i.e. a RESTful controller alone).

Maybe I just don't get it, but for me RESTful design is a nice academic idea, but not so relevant in the reality. At least with current web design standards.

What are your ideas ?

Re: What's the real advantage of REST ?

I know how you feel.

I found this on wiki which helped me a little
http://en.wikipedia.org/wiki/Representa … d_Benefits

--

Re: What's the real advantage of REST ?

I understand these benefits and I agree for simple hypertext systems, but I don't see them in real world state-of-the-art web applications.

As long as the object-of-interest is one simple model object, the approach seems to be nice, but as soon as one tries to manipulate parts of objects or graphs of objects then this approach doesn't fit IMHO.

However, this screencast http://peepcode.com/products/restful-rails helped me a lot and I'll give RESTful design a second try in my next app.

Re: What's the real advantage of REST ?

REST isn't for every application, but I think a good 80% of the Rails apps out there can benefit from it. I know for me it was a paradigm shift in how I approached web development. It requires a different way of thinking about the application.

Here are some of the benefits I'm finding of REST:

1. The biggest benefit for me is consistency. Mostly all other benefits stem from this.
2. Because of the consistency, things become much simpler and easier to understand.
3. It's easier to read and understand code by other developers if they've followed REST.
4. Permission handling is easier. It can usually be abstracted into before filters because of the consistent naming of actions.
5. Almost free and predictable API
6. No more multiple scopes in controllers.

If you're struggling with adopting REST, here's a few tips:

1. Not every controller needs to be RESTful, if it doesn't fit, just make it a normal controller.
2. Not every RESTful controller needs all 7 CRUD actions. Many of mine don't have all 7.
3. Design the interface first. Everything should be based off of the interface. If you create the models before the interface is designed you will have this big gap between the two and REST will be difficult to do.
4. After you've designed the interface, look for CRUD patterns. Whenever there's a form, ask yourself if it's creating or updating something. This is a shadow of where a model should be.
5. Don't be afraid to fill up your routes.rb file. Not everything fits perfectly into the 7 CRUD actions.
6. IMO it's okay to handle multiple models in a CRUD action as long as there's one primary model. For example, if you want to create an Address along with the User on the registration page, go ahead and do that in the UsersController.
7. Don't get too caught up in nested resources. They can be nice to an extent, but I think they are overrated.

Hope that helps.

Railscasts - Free Ruby on Rails Screencasts

Re: What's the real advantage of REST ?

In developing my first "RESTful" application, what I don't understand is what REST has to do with any of it, so far the benefit to me is it is becoming a "CRUDful" application. After watching DHH's keynote, the focus seemed to be CRUD, and about richer models popping up when you start thinking CRUD, and that definitely seems to be true, and it has helped the design of my application considerably. But why REST? I was doing CRUD before wasn't I?

As far as I can tell, and correct me if I'm wrong, but all of these benefits of "REST" have nothing to do with REST and can be done without it:

1. CRUD
2. respond_to
3. nice URLs (weren't they fine before? and couldn't you have always cleaned them up with a few extra routes anyway?)

So what benefit does all this hassle give me?

1. Nested resources, and their caveats, when I could pass the foreign key in a GET anyway?
2. Two meaningless actions in every controller ('new', and 'edit') just to keep in line with the HTTP verbs?
3. Faked HTTP verbs everywhere.. what about ajax and having to make everything buttons for that hidden form variable?
4. Constant design issues, interrupting my flow to ask what HTTP verb describes a search or rating a resource in a CMS
5. Things like restful_authentication - a login is a create and a logout is a destroy? Why?
6. Creating and managing multiple models in one controller (which is just about every controller unless all you write is 15 minute screencast blogs) making me stop and think some more

I really agree with DHH's comments about contraints and REST forcing you to think in terms of CRUD...Yeah, I made the subscriptions model, the memberships model, as well as several others, and CRUD controllers for them, and I love it. But what did that have to do with REST? It seems to be getting in my way for the other half of my application which are operations not quite related to CRUD.

Am I missing out on some "REST magic" here?

Re: What's the real advantage of REST ?

heyaz wrote:

Am I missing out on some "REST magic" here?

Not really. I agree with most of what you are saying. But I'll try to address some points in detail.

heyaz wrote:

As far as I can tell, and correct me if I'm wrong, but all of these benefits of "REST" have nothing to do with REST and can be done without it:

Right. I agree that the biggest benefits REST gives you can already be accomplished without REST. I love the consistency that CRUD provides and consider this the biggest benefit.

Probably the next benefit for me is named routes. You don't need a long hash of controller/action names. I mention this in the latest Railscasts episode. While you can do named routes without REST as well, it just takes a lot more code.


heyaz wrote:

1. Nested resources, and their caveats, when I could pass the foreign key in a GET anyway?

Agreed. I honestly don't use nested routes. It does produce some cleaner URLs, but not enough for me to use them.

heyaz wrote:

2. Two meaningless actions in every controller ('new', and 'edit') just to keep in line with the HTTP verbs?

I don't think they did this to keep in line with HTTP verbs. They were doing this way back in the first scaffolding. I consider it more of a separation of concerns. If you try to merge this all into one action it can get pretty complex with a lot of "if" conditions. Separating it out has its downsides too, namely having to recreate all instance variables the template relies on if there is a validation error. There are various ways to get past this though.

heyaz wrote:

3. Faked HTTP verbs everywhere.. what about ajax and having to make everything buttons for that hidden form variable?

I'm assuming you are referring to the requirement of javascript or form buttons when creating/updating/deleting a resource? This is recommended even without REST because you don't want GET requests to do something destructive.

heyaz wrote:

4. Constant design issues, interrupting my flow to ask what HTTP verb describes a search or rating a resource in a CMS

I look at it more as a "nudge" saying there's an alternative design decision which is often the better way to go. If you need to add another action to a controller which doesn't fit into CRUD, you can often move this into its own resource which may result in a better design.

I know at least for me, if I didn't have this constant constraint I would be too lax and say "oh, it's okay to just add this other action" and before I know it I have this fat controller dealing with multiple types of resources.

heyaz wrote:

5. Things like restful_authentication - a login is a create and a logout is a destroy? Why?

Not everything fits in REST. I consider this borderline. It does make some sense if you think about dealing with a "session" resource and you are creating/destroying that.

heyaz wrote:

6. Creating and managing multiple models in one controller (which is just about every controller unless all you write is 15 minute screencast blogs) making me stop and think some more

This kind of goes back to point 4. IMO managing multiple resources in one controller is not that bad until you need to create separate actions for that resource. This is where a second controller needs to come in to keep every controller dealing with only one scope.

Railscasts - Free Ruby on Rails Screencasts

Re: What's the real advantage of REST ?

Thanks for the reply smile

Guess it just feels a bit painful at first, especially when I was already pretty into CRUD before, always trying to separate controllers and keep the scope as close to one model (or resource) as possible. But I will admit the constraints of REST did clean things up a bit more and reveal some hidden associations.

I guess my main concern now is how and when to break out of "REST" and HTTP verbs, and the most elegant way to do so. Most of the blogs/articles/forum posts I read about this topic seem to over emphasize staying RESTful when I think they really just mean CRUDful... to the point where I feel like I'm going to break something when I add an action that doesn't fit the pattern and use a carefully chosen HTTP verb.

Last edited by heyaz (2007-05-22 12:57:16)

Re: What's the real advantage of REST ?

I'm anxious to see this presentation from Railsconf 2007... seems to address a lot of my concerns.

http://www.web2expo.com/presentations/r … _scott.pdf

Re: What's the real advantage of REST ?

I attended the talk. It was pretty good, but covered more of the theories behind REST rather than practical uses for it when it comes to Rails. I can share my notes on it if you're interested.

Railscasts - Free Ruby on Rails Screencasts

Re: What's the real advantage of REST ?

Yeah, I attended it as well. It had a lot of theory behind it, but nothing I could really use in a practical sense.

If you'd like my notes on it as well you could pick up my "mind map" off of my blog, which also critizes the session. (Railsconf Day 2)

Combine Ryan's and mine, and you may get a lot of information at once! Ha.

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: What's the real advantage of REST ?

I would like very much to have the notes !

Re: What's the real advantage of REST ?

Jeff Cohen has a 5 Part series on REST that you might want to check out too.

http://www.softiesonrails.com/2007/3/28 … -resources

<= Will buy you beer for some Ruby Help

Re: What's the real advantage of REST ?

There was a statement from the 06 conf talk on REST that really resonated with me.  Paraphrasing,  David said that REST isn't necessarily about reducing everything to CRUD, but rather to force developers to think more about their domain model.  I am currently designing an app for the new facebook implementation and planning to do a completely restful design.  Keeping the CRUD goal in mind helped me to really establish the inner workings of my system and create a modularized implementation that keeps my conrollers beautifully skinny.

However, in line with ryanb's continued statements about REST only applying to 80% of the whole project, I have a few controllers that completely shuck the REST protocol.  Part of my application involves comparing the scores of two people and displaying some...

Interesting, as I was typing the above paragraph I thought a little harder and came up with a REST implementation of the particular set of actions I was about to describe.

I suppose for me, it breaks down to one easy qualifier.  Is the concept I am attempting to model contain some form of data retention(i.e. db storage, session storage, etc)  If so, REST is usually the best way to go.  However, if you find yourself attempting to apply REST to presentation logic, then perhaps a different approach is a good idea.

Re: What's the real advantage of REST ?

heyaz wrote:

I'm anxious to see this presentation from Railsconf 2007... seems to address a lot of my concerns.

http://www.web2expo.com/presentations/r … _scott.pdf

This explains the "what" a lot, but not really a lot of the "why".

What shocked me was that sentence about the "new way" of handling cookies: storing sessions entirey in cookies, instead of just a key to server-side data. The author remarks that "this rocks"
Because "the session based auth is REST-ful"

?!?!?!?

I think that is REST-fundamentalism. Why would one want to store actual data in a cookie? IMO there are only trivial cases that would NOT open up all kinds of problems, some of them severe security problems.
Am I missing or misunderstanding anything here?

Re: What's the real advantage of REST ?

I hear storing the session data in the cookies is now the default in edge rails. I'm not sure if this is a good thing or a bad thing. It is hashed, but I'm still concerned about security.

I personally don't see the point either. It does save you the headache of managing the session store on the server, but I think it opens just as many headaches.

The only benefit I see is it forces you to minimize the use of the session and to store only a few things in the session (model ids). Which I think is a best practice.

Sorry this is getting off topic...

Railscasts - Free Ruby on Rails Screencasts

Re: What's the real advantage of REST ?

*off_topic* What are you making for facebook? I'm really interested in the new developer platform, but I still have lots of unanswered questions about how to make it work.

http://www.brianthecoder.com

Rails, Javascript, Actionscript and More.

Re: What's the real advantage of REST ?

Man I am wrecking my brain right now with REST and routes.

It basically broke a ton of ***** when I tried to implement it, I got my tests fixed... for the most part: this is what I did.

I have users, and posts, I wanted to be able to view all posts by a user - so understanding there is only 7 actions (wanted to stay true) i was asking myself how I would "list" them, so then came the idea of a nested resource. /users/1/posts

All good right? That's the point of a nested resource, otherwise I'd have to create a list action, no?

What if I wanted to add comments to posts, what would I do with the routes?

Arg, I thought I knew REST pretty well, in comes routes and I go bonkers!

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: What's the real advantage of REST ?

DFischer wrote:

I have users, and posts, I wanted to be able to view all posts by a user - so understanding there is only 7 actions (wanted to stay true) i was asking myself how I would "list" them, so then came the idea of a nested resource. /users/1/posts

That's how I would do it, too.

What if I wanted to add comments to posts, what would I do with the routes?

You can nest it one level deeper, if you like, but that depends on your model. I guess that every user may add comments to a post, so this is most probably not an action that is specific to the posting's owner. In my models I try not to nest resources deeper than one level if possible.

So I would add another route, where the post is top level and the comments are a post's nested resource.

Depending on your requirements this may lead to some security issues, as it's easy now to access arbitrary posts just by their id and not only a user's own posts.

Another solution would be to break REST in this specific case and add a simple named route with a customized action just for adding a comment.

Re: What's the real advantage of REST ?

As mentioned above, I don't use nested routes. If that's what you're getting hung up on, don't use them for now. You can do everything without them - it's just the URL isn't as pretty. For example:

/users/1/posts
# vs.
/posts?user_id=1

They both have the same effect. If you don't use nested routes you aren't forced to pass the user_id in times you don't want to:

/users/1/posts/5
# vs.
/posts/5

I recommend not using nested routes at the beginning. If you want cleaner URLs you can add them back, but you will have tackled most of REST by then. Nested routes have a tendency to distract you from the important parts of REST IMO.

Railscasts - Free Ruby on Rails Screencasts

Re: What's the real advantage of REST ?

smizzle wrote:

*off_topic* What are you making for facebook? I'm really interested in the new developer platform, but I still have lots of unanswered questions about how to make it work.

I am going to be spending most of the day getting this up and running, I will post a new thread about it when I get some real work done.  Sorry all for the off topic discussion. smile