I'm wondering if there's a specific programming principle (Demeter?) that supports the idea that Rails helpers should never use controller instance variables, rather, they should receive such variables as function parameters. For example, assume my ChickensController#squawk
action creates an instance variable called @egg
. Furthermore, assume the squawk
view contains a call to a helper called cockadoodledoo
, implemented like so:
def cockadoodledoo @egg.to_s end
Would it be better or unnecessarily verbose to pass @egg
as a parameter, such that the view calls cockadoodledoo(@egg)
and for the helper to resemble:
def cockadoodledoo(egg) egg.to_s end
I hope one of you happy hackers is bored enough on a Friday afternoon to assert an answer. Cockadoodledoo!
This question here is similar, but was never accurately answered.
Receive them as a param. Otherwise, as the app grows, it gets very difficult to trace where the instance vars are being set when refactoring, troubleshooting, etc.
Also, I believe there's a general best practice to only use instance vars in views within the initial template...and from there you should pass the var into helpers and other partials.
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