开发者

How to map non-REST URLS to REST ones?

I have a small ra开发者_C百科ils 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?


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.


What if you do some url rewrite in apache ?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜