views:

50

answers:

3

I have a small rails app that has default scaffold generated routes eg. /stadia/1.xml. However I have to support legacy client app that can't construct such URLs correctly. What I need to do is to map URL in the form:

/stadia?id=1?format=xml to /stadia/1.xml

Or even something like:

/myApp?model=<model_name>?id=<id>?format=xml to /<model_name/<id>.xml

Is it possible to craft appropriate route in Rails?

A: 

What if you do some url rewrite in apache ?

dzen
A blanket statement like this does not help the OP much. I would be helpful to provide at least an example of how to achieve this.
dafmetal
Sometimes, people works with old existing code, and can't deal with "oh why didn't we rewrote the whole app, and the whole urls in our app ?" It's a workaround, so, but it's quick, and ugly.
dzen
A: 

I had a similar question. No answers so far, so it seems routes.rb config doesn't offer an easy way of doing this (routing based on query parameters), which I find surprising actually.

So an ugly workaround would be to have a 'myApp' default route, and then have a special redirecting controller which would look at the query params (because in controllers you do have access to that) and redirect accordingly.

Harry Wood
+1  A: 

I don't have good answer for this. What I would do is change first part of url to /stadia_legacy for legacy urls or change first part of urls for RESTful routes.

Then you can map in routes:

map.stadia_legacy :stadia_legacy, :controller => 'stadias', :action => 'please_redirect_me'

Then in stadias controller in action please_redirect_me you can check all params (they are availble in params hash: params[:id], params[:format] etc.) and redirect to correct url. Or you can write all routes manualy to correct controller and action.

klew