I am thinking about having different site sections of a Rails 3 webapp. The site sections should show the same resources using the same controller and the same views.
So http://foo.org/premium/products/1 should present the same product as http://foo.org/products/1 (maybe with some modifications dependent on the section).
For handling that I thought about rewriting the URL somehow (maybe in routes.rb, at the Rack level or the server level), so that http://foo.org/premium/products/1 becomes http://foo.org/products/1?section=premium. Are there any better solutions for that?
But the real problem I see is when generating the links of those sites as those should retain the section. E.g. a link on the site http://foo.org/premium/products/1 to the product with id 2 should be http://foo.org/premium/products/2 and not http://foo.org/products/2?section=premium.
Unfortunately this is not so trivial as link_to and *_path don’t know about the sections. I also want to make this highly dynamic so that I can easily add and remove sections without touching static routes.
I thought about rewriting the URLs after they were generated by link_to. Would this be a good approach? Any other suggestions?
Finally I chose the approach of rewriting the URLs as there are only minor changes between those site sections (e.g. another background). That way I could reuse all MVC classes instead of creating new stuff when using for example routing namespaces.
For rewriting the URLs I use Rack middlewares. All incoming URLs are rewritten by rack-rewrite. Links of the response body are rewritten by another custom middleware (something like done in this Railscasts transcript.
So URLs like
/premium/products/1will end up as/products/1?section=premium(and vice versa).