When implementing some helper methods, sometimes, I want to store some computed result somewhere accessible from the helper method as a cache.
If I store it into instance variable, it will pollute instances, so it looks like not wise to do that.
Any good place to store such value? Or doing such heavy weight computing in a helper is a bad idea?
Sometimes I use Rails cache to store this kind of values. The code is like this,
def helper_method
Rails.cache.fetch('helper_value') do
# calculate the value if it does not exist
...
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With