Just another "why is it that way" question: I noticed that private helper methods still can be accessed within views. Why's that? And is there a way to prevent this (e.g. when having helper methods that should only be called from within another helper)?
You can put a method in a controller and then call helper_method in the controller to indicate that this method is available as if it were in a helper too. This method will then be available to all controllers and to all views.
Helper methods are generally public and static so that these can be invoked independently. Each methods of a helper class should work independent of other methods of same class.
1 Answer. I prefer such helper methods to be private static; which will make it clear to the reader that they will not modify the state of the object.
Helper methods are often declared as public static so they can be invoked with the class name like Classname.
Helpers are modules that get mixed in to the views. This means that public, protected and private methods in the helper become public, protected and private methods on the views.
I don't think that you can actually hide the helper methods from the view. You'd need to do something like have a helper class which you instantiate in the helper and then delegate calls to that - sounds like it could get messy fast though. :)
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