0

In Rails 4 application I need to reload routes (routes.rb) for every request before the application uses it. Where should I put MyApplication::Application.reload_routes!
I have tried to put it in config/applicaion.rb,config/environments.rb but it's not working.
I have also tried to put it in before_filter of ApplicationController and it's also failing. RoR api is brief on how to use it.
So any assistance/guidance on this will be greatly appreciated

James Mwaiponya
  • 77
  • 1
  • 10
  • The routes are dynamic – James Mwaiponya Jul 14 '14 at 09:23
  • I doubt that is possible. But i also doubt that routes must be dynamic. Code should be dynamic, route can be the same. Could you plz specify the concrete usecase – dre-hh Jul 14 '14 at 19:19
  • For instance if you have your controllers stored in a table in a database.For every request you have to query from the table for specific controller depending on url parameter(s).Yes routes must not be dynamic but can be dynamic. See this [link](http://archive.gibberishcode.net/archives/creating-dynamic-routes-at-runtime-in-rails-4/130) – James Mwaiponya Jul 15 '14 at 08:35

1 Answers1

1

Seriuosly i would never do that. It is massive intervention into Rails internals. Just configure one dynamic route to a single controller. Then load some Command(Service) according to the dynamic part of the url from the db, deserialize, instantiate and pass the current controller as a reference to this dynamic service constructor.

The dynamic service you are loading should be implemented according to the following refactoring technique:

Replace Method with Method Object

Another Option would be to load a lambda/Proc because it would automatically have the controller scope once instantiated

dre-hh
  • 7,840
  • 2
  • 33
  • 44
  • I am trying out your answer.However I need a straight forward answer for the question. Is it possible to reload routes ( routes.rb) just after a request (Yes or No ?).If your answer is Yes show me an example and if your answer is No then in what circumstances would you reload routes(routes.rb) for RoR application. – James Mwaiponya Jul 17 '14 at 07:08
  • Yes, you can reload routes after a request and have new routes on the following request. However in question you've asked `for every request before the application uses it`, which is not possible. You could reload the routes, then redirect immediatly to the new route, but this would be 2 requests. I would only reload the routes if the are some sort of configurable by the admin at well defined periods. http://stackoverflow.com/questions/129510/is-it-a-bad-idea-to-reload-routes-dynamically-in-rails – dre-hh Jul 17 '14 at 07:34
  • In order to provide you an example, you have to create a new question, describing the concrete problem. I bet, there is a solution without reloading the routes on each request – dre-hh Jul 17 '14 at 07:35