I have a View that can vary significantly, depending on the ‘mode’ a particular user has chosen.
I thought I would extract the different behavior into two different Helpers, and then have code like this in the Controller:
class MyController < ApplicationController case mode when 'mode1' helper 'mode1' when 'mode2' helper 'mode2' else raise 'Invalid mode' end etc...
Once the correct helper is loaded, then a statement like <%= edit_item %> , which is defined in both helpers, will load the correct form for the particular ‘mode’.
This works beautifully in development, but in production, the case statement is only run once. Then you are stuck with whatever helper was first loaded (Duh! I should have known that.)
I have thought of other ways to achieve what I need to do, but I still think this use of Helpers is a nice clean way to change behavior of a View.
Does anyone know how I can load (or reload) a helper at runtime?
TIA: John
I can think of a few ways to do this, but not sure about loading modules as you were suggesting.
Load different partials and choose which to load based on the state.
Or if you wanted to keep that logic out of the view (which might be a good thing), you could put something in your controller to render different actions based on the mode:
Which seems much better than putting that logic in the view.