almost 2 years ago

参考Rails Model, View, Controller, and Helper: what goes where?

To add to pauliephonic's comment:
Helper: functions to make creating the view easier. For example, if you're always iterating over a list of widgets to display their price, put it into a helper (along with a partial for the actual display). Or if you have a piece of rjs that you don't want cluttering up the view, put it into a helper.

Helper vs Model? Which should I use?
First you must ask yourself what is the difference between a model and a helper. The most common answer is "a model has a table behind it". Then ask yourself "why are Observers listed as models and not helpers".

Helpers should not exist. But the most common practice is.. when you don't know where to put some code, you put it in a helper.
This is wrong, in my opinion. Using helpers is not really in the OOP spirit. You are just grouping some independent functions inside a class.

But enough philosophical talk.
I would use a model. Mainly because helpers are always singletons. Mage::helper() always returns the same instance of a helper class.
For models you can get new instances and singletons, depending on what you need. So it's a bit more flexible using a model.

But in this specific case, if you only need one instance of your class you can use either a helper or a model. There is no difference. Just what makes you comfortable.

helper_method: 将controller中定义的共享给view和helper使用,在controller之外可以使用该方法,而不必再写一次

← 今日学习orid 20170208 session是什么? →