Hello
I suppose you all know that Simply Restful plugin has made its way to
the Rails Core. I like the idea of REST, but for me the way this plugin
rest-enable Rails is deficient. It’s a bit too much of “convention over
configuration” - we’ve got convention, but there’s not enough
configuration for those that don’t like the defaults. My suggestions:
- Mappings in routes.rb: url_path(may_contain_regexp) <-> controller .
“GET: /users/1” isn’t enough, I want “GET: /users/friends/jesse”
- Ability to implement other http methods (http, webdav, custom [YES,
why not?])
- Arbitrary content types for the resources (XHTML, XML, but also plain
text and binary)
In my opinion only this design is generic enough to suit anybody’s
needs. Now, I’m not a Ruby hacker, I can also try to show the best way
to do it. And so I did.
Regards
Havocado
I’m totally not an expert with this, but I think you can do the
/users/friends/jesse style routes. There’s functionality for nesting
resources which will do that. There’s also a prefix option I believe.
As for the id, one way is to override to_param in your model, as such:
def to_param
“#{id}-#{name[0,40].gsub(/[^a-z1-9]+/i, ‘-’)}”
end
I didn’t write that, and I forget who did, but basically that will give
you id’s that are id#-the-name-of-the-record. Replace “name” with
whichever attribute you want to use as the “name.”
Having said all of that, I kind of agree that the new stuff seems a bit
too strict and only works well when you commit to it completely. I’ve
being doing a mix and match in my new app and on some days I think
Simple RESTful is the greatest thing ever, and on other days I’m
pulling my hair out. I’d say there’s more to love than not though.
Cheers,
John
- Mappings in routes.rb: url_path(may_contain_regexp) ↔ controller .
“GET: /users/1” isn’t enough, I want “GET: /users/friends/jesse”
You can do this now with /users/jesse/friends
map.resources :users do |user|
user.resources :friends
end
No one ever said that resources have to map to a model necessarily, or
that the :id param has to be an integer.
- Ability to implement other http methods (http, webdav, custom [YES,
why not?])
I’ve heard of webdav implementations in rails, it’s possible.
- Arbitrary content types for the resources (XHTML, XML, but also plain
text and binary)
You can easily register your own mime types. I believe it’s
Mime::Type.register…
Though simply restful’s design is pretty flexible, you’re correct that
it won’t handle every case out there. It’s not meant to. There’s no
reason you couldn’t just write your own routes for whatever restful
layout you want. And if you’re using it multiple times, make a small
plugin out of it. That’s how these things start.
–
Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com