/ rails

Reload or Add routes at runtime in Rails 3

In one of our eCommerce app, we needed SEO friendly Menu urls like /brandy/cognac. For this, we added a generic route /:parent/:child/:grandchild at the bottom of config/routes.rb.

With this approach, there was an issue with invalid urls like /this/is/wrong being redirected to specified controller action.

To fix this, we used Menu names to generate specific urls. But the routes would become invalid if Admin changes the menu name.

So we needed a way to reload routes once the menu is updated.

I found a hack mentioned in [this blog(Blog)][1] that allow us to add routes dynamically and also reload the routes.

We tried this and it worked.
[1]: http://experiments.openhood.com/rails/rails 3/2010/07/20/add-routes-at-runtime-rails-3/