Memoized helper methods instead of instance variables in controllers

This is sort of a personal question to @sgrif but might be of general interest so I’m posting it here. I recently discovered the Bikeshed podcast and have been going through the episodes (starting with number 1). On at least two separate episodes Sean has said rather contentedly that he doesn’t like to pass instance variables to views, he prefers to use memoized helper methods. I know what memoized means and I know what helper methods are, but I’m not sure where you would put those methods, or how they would look.

Anyway I’d love to see an example of what you are talking about because I agree with the general philosophy of what you are saying.

I love the podcast.


Here’s a gist that gives an example of using memoized helper methods in a controller:

It turns out ActionController has a helper_method class method that makes methods available in the views as if they were defined in an actual helper module:

You can memoize the results of private methods in the controller and then make those methods available to the view as helper methods.

That’s super helpful @davidcpell, thanks!

I agree with avoiding instance variables in views.

I tried the memoized variables approach for a while, but I’ve come to prefer explicit render calls instead. Here’s an example: