RESTful services that always work

From: Loqi
Sent on: Thursday, January 31, 2008 12:56 AM
I'm beginning to get more comfortable with the idea of making a 
plugin for pluralization-indepe­ndent Restful services. I'm just too 
green with Rails to do it alone. I don't know how.

I'm pretty sure everything the scaffold generator is producing is 
sound. I have some inkling that the entire problem lies with the 
map.resources method. But I can't quite find the exact failure. The 
routes it produces seem to be named in a silly way, but I can't see 
why those silly names don't work. Here's as far as I got:


Routes generated by map.resources for an uncountable-noun-nam­ed model:
fish_index     GET  /fish             {:controller=>"fi­sh", :action=>"index"}­
new_fish       GET  /fish/new         {:controller=>"fi­sh", :action=>"new"}
edit_fish      GET  /fish/:id/edit    {:controller=>"fi­sh", :action=>"edit"}
fish           GET  /fish/:id         {:controller=>"fi­sh", :action=>"show"}
. . .
(This set crashes when you try to use the routes.)


Pluralizing-noun-nam­ed models get this:
carps          GET  /carps            {:controller=>"ca­rps", :action=>"index"}­
new_carp       GET  /carps/new        {:controller=>"ca­rps", :action=>"new"}
edit_carp      GET  /carps/:id/edit   {:controller=>"ca­rps", :action=>"edit"}
carp           GET  /carps/:id        {:controller=>"ca­rps", :action=>"show"}
. . .
(This set is the regular Rails "convention over configuration" routes 
that don't crash.)


My fantasy is to generate RESTful conventions that are consistent, 
non-subtle, and work every time, no matter what:
fish_index     GET  /fish/index       {:controller=>"fi­sh", :action=>"index"}­
fish_new       GET  /fish/new         {:controller=>"fi­sh", :action=>"new"}
fish_edit      GET  /fish/edit/:id    {:controller=>"fi­sh", :action=>"edit"}
fish_show      GET  /fish/show/:id    {:controller=>"fi­sh", :action=>"show"}
. . .
(This set doesn't exist, but probably should.)


But I don't think the weird route names are actually the problem. I 
don't yet know enough about Rails to find where it's falling on its 
face. I tried the solution described here...
http://somethingl...­
...where it says to provide an explicit or contrived singular noun to 
the resources method call in routes.rb, like this:
map.resources :fish, :singular => :fish_instance
This would be a stopgap measure until I can do something more 
satisfying, but I couldn't actually get it to work, and I'm not sure 
why. I'll keep prodding at my personal problem until I find some good 
workaround.

Anyway, I'd like to fashion some kind of general plugin solution that 
would help anyone (especially me) who wants to use RESTful Rails with 
a declawed inflictor. It doesn't seem too difficult if you know how 
to do it. But I guess nothing is difficult if you know how to do it.

If some kind soul out there is willing to help this thing happen, I'd 
be most grateful for some mentoring guidance while I puzzle it 
through. Or maybe the other way around; If this is easy for an 
experienced person to just do, I'd like to watch and learn. Please 
contact me or this listserve if you can offer some help to a fellow 
traveler in distress.

Loqi

And hey, thanks for the tip on The Rails Way, Rich. I'll look for that one.

Our Sponsors

People in this
Meetup are also in:

Log in

Not registered with us yet?

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy