0

I barely use them in my projects. Is this normal? If I need to return just one line of html, say an image tag for a gravatar, then a helper is great:

def gravatar_for(user, height=90, width=90, alt=user.name + "'s gravtatar")
    gravatar_address = 'http://1.gravatar.com/avatar/'

    clean_email = user.email.strip.downcase 
    hash = Digest::MD5.hexdigest(clean_email)

    image_tag gravatar_address + hash, height: height, width: width, alt: alt 
end

However, if I want to loop through some errors, and return html structure rather than a single line, it quickly gets difficult.

Is a good rule of thumb if you want to return one line of html then use a helper, else use a partial to return more complex html?

Starkers
  • 10,273
  • 21
  • 95
  • 158

1 Answers1

0

First of all, the question is opinion based. So, most likely it will be closed.

Here are several questions on similar subject:

DRYing rails view: partial vs helper Why shouldn't Helpers have html in them?

Generally speaking, helper is just a way to

  • Have DRY views
  • Make clean views and controllers

Generally speaking you can DRY views by moving out some duplicated functionality to controllers. However, as result controllers got cluttered. So, the better way to keep view related helpers in helpers (vs controllers), which you already figure out.

Some Helpers don't touch HTML at all (as example, they may just provide some formatting of numbers etc) or they may generate just one tag (as in your example) or generate piece of HTML code. However, as soon as you are talking about some piece of HTML code, you rather use partials vs helper.

Community
  • 1
  • 1
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184