Manual route redundancy with .:format (REST from scratch)

I am building an RESTful search application in Rails. Unfortunately I
can’t take advantage of map.resources routing because the application
has no models (it communicates with a SOAP service). So I am building
a REST architecture from scratch.

I have 5 custom routes to emulate RESTful nesting of resources.
However, I have to duplicate every entry to be able to catch whether
or not the resource is requested with a format. So I have to have 10
route entries…

map.connect ‘parent/:parentid/child/:childid.:format’,
:controller => ‘child’,
:action => ‘show’,
:conditions => {:method => :get}

map.connect ‘parent/:parentid/child/:childid’,
:controller => ‘child’,
:action => ‘show’,
:conditions => {:method => :get}

Does anybody have any suggestions on how I can set this up so I don’t
repeat myself?

Cheers,
Stephanie

On 6/13/07, stephanie [email protected] wrote:

I am building an RESTful search application in Rails. Unfortunately I
can’t take advantage of map.resources routing because the application
has no models (it communicates with a SOAP service). So I am building
a REST architecture from scratch.

map.resources is not tightly bound to models (or the naming convention
for controllers, as it happens)

map.resources :wibbles, :controller => ‘something_arbitrary’

your something_arbitrary_controller.rb can do whatever it wants - it’s
your code.

HTH,
Trevor

Trevor S.
http://somethinglearned.com

Thanks for the reply. I used map.resources and it worked fine until I
began nesting my resources. The syntax for nesting map.resources does
not seem to work without models. The issue I have with map.resources
is it creates several routes which aren’t applicable to my
application. For instance, I have a few controllers which should only
be accessible with a GET since they are results from a web service.
The POST, PUT, DELETE + new and edit are not needed.

-Stephanie

On 6/13/07, stephanie [email protected] wrote:

Thanks for the reply. I used map.resources and it worked fine until I
began nesting my resources. The syntax for nesting map.resources does
not seem to work without models. The issue I have with map.resources

I’m not sure how it wouldn’t work without models - as I said,
map.resources has nothing to do with models really.

I’ll hazard a guess tho: is it something like you want /foo
(foo_controller) and /foo/bar (bar_controller)? If that’s what you’re
talking about, then /foo is a map.resource (note the singular).

So unless what I’ve said above (or what you said below) explains why
it doesn’t work for you, maybe you could expand on this a bit?

is it creates several routes which aren’t applicable to my
application. For instance, I have a few controllers which should only
be accessible with a GET since they are results from a web service.
The POST, PUT, DELETE + new and edit are not needed.

Yeah, I can understand why you wouldn’t want them. I guess it’s a
tradeoff between the convenience of map.resources with a controller
that only has index/show methods vs the extra (and un-used) routes and
url helpers.

If the extra routes and helpers really offend you then you could whip
up a little helper or two that generates routes the way you want -
after all, that’s what map.resources is (see resources.rb in the rails
source).

HTH,
Trevor

Trevor S.
http://somethinglearned.com

stephanie wrote:

Thanks for the reply. I used map.resources and it worked fine until I
began nesting my resources. The syntax for nesting map.resources does
not seem to work without models. The issue I have with map.resources
is it creates several routes which aren’t applicable to my
application. For instance, I have a few controllers which should only
be accessible with a GET since they are results from a web service.
The POST, PUT, DELETE + new and edit are not needed.

I’m curious why this would be a problem? If you don’t define the
corresponding methods in your controller you should be fine? Or am I
missing something?


Cheers,

  • Jacob A.

Thanks, this discussion has been really helpful.

I am not sure why it didn’t work when I nested it. Perhaps it has to
do with mapping associations in the resources.rb. It was awhile ago,
so there might have been singular issue. At this point I think I’ve
decided I want to implement my own routes. I might refer to
resources.rb to create my own helper.

Rails map.resources is a great template for how I’d want to do RESTful
routes 95% of the time. However, there are times when the conventions
breakdown for a particular resource-oriented service.

-Stephanie