Readers: 20 | Updated: 06-25

What belongs in a helper method?

Translate Into:

I’m working on some improvements to Basecamp, specifically the screens where you manage which people have access to a project. There’s an area on our new template with checkboxes beside peoples’ names so you can check which people should be added to your project. I want to apply a class name to the label tag around the checkbox for each person. So I pulled up the template and searched for “label” to find where I might add the class name. There were no matches. So I dug deeper, and saw that the HTML for the label, checkbox, and person name is being generated by a Rails helper method.

This is the template code that calls the helper method.

 <% people_without_access_from(company).each do  person  %>   <%= add_person_to_project_check_box(person, company) %> <% end %> 

Next I pulled up the helper method to see if it really is responsible for producing a chunk of HTML with the label and checkbox.

 def add_person_to_project_check_box(person, company)   content_tag(:label,      check_box_tag("people_ids[]", person.id, false, { :class => "company_#{company.id}_person" }) +     " " + person.name   ) + tag(:br) end 

Yup, it’s generating the HTML. Right away this smells bad to me. The helper is first generating a label tag. Inside that label, there is a checkbox followed by a space character and the person’s name. Finally a break is appended after the label. This smells bad for two reasons. First, it’s just not so nice for helpers to cook out HTML when they don’t have a good reason to. Second, it’s harder to locate and change HTML when it’s hidden inside a helper.

Returning to my original goal, I wanted to add a class name to the label around this checkbox. If I add the class name to the existing helper, it’s going to get even more messy and complicated because I have to give content_tag an attribute with the class name I want. It would look like this:

 def add_person_to_project_check_box(person, company)   content_tag(:label,      (check_box_tag("people_ids[]", person.id, false, { :class => "company_#{company.id}_person" }) +     " " + person.name     ), :class => 'checkbox') + tag(:br) end 

To find a better solution, we should rethink what the helper should be responsible for. Helpers are useful when they hide complexity that isn’t relevant to the template. Looking at this helper method, I see that it’s useful to hide away check_box_tag with all those params. But the label, the break, and even the person’s name could all be in the HTML and the template would be clearer. Let’s do that.

Here’s the new helper. Now it only produces the checkbox.

 def add_person_to_project_check_box(person, company)   check_box_tag("people_ids[]", person.id, false, { :class => "company_#{company.id}_person" }) end 

And here’s the updated template code, with the label, person name, and break moved over.

 <% people_without_access_from(company).each do  person  %>   <label class="checkbox">     <%= add_person_to_project_check_box(person, company) %> <%= person.name %>   </label><br /> <% end %> 

Now that’s a lot easier to read. Generally speaking, it’s a good idea to keep your HTML in your templates and out of your helpers. Helpers are useful when they hide implementation details that are irrelevant to your template, or when they allow you to abstract common template code to avoid repetition. If you find yourself generating a lot of HTML in a helper, think twice and try to keep as much HTML in your template as possible.



From The Blogs

Goal Setting College

05-01
How To Do Well In Something Even If You Don’t Seem To Have The Talent For It
One of the common grouses I hear, hindering people from going into a particular challenge is that they believe they dont have the X-factor to excel in it. Usually, its after one or two failed attempts... 查看全文

Wake Up Later: Freelance + Passive Income

04-11
Client Communication: The Most Important Part of Freelancing
As a solo freelancer, the easiest and most consistent way to find work is to connect with design or marketing firms who are looking for reliable contractors. In fact, over half of my own clients are f... 查看全文

Wake Up Later: Freelance + Passive Income

04-11
Less is More: 10 Methods to Be More Productive, More Profitable, and Happier
One of the most frustrating paradigms in the modern workplace is that "staying busy" is often substituted for "staying efficient." You can stay busy for eight hours and get very little work done (we a... 查看全文

Personal Development with The Positivity Blog

04-18
7 ways to move beyond procrastination
Almost everyone is held down by what some call “the silent killer”. Procrastination strikes everywhere. We all want to avoid the pain or discomfort of doing something we feel is boring, stupid, pointl... 查看全文

Ririan Project

04-10
8 Powerful Ways to Rediscover Your Passion
“Without passion man is a mere latent force and possibility, like the flint which awaits the shock of the iron before it can give forth its spark.”- Amiel, Journal, 17 December 1856 Note: This guest p... 查看全文

Gearfire Productivity

04-10
Tips for Learning a Foreign Language
(photo by Maria Schwartzman 2008)It's a fact: the world is globalizing. This means that people not necessarily speaking the same language must come together to communicate. In this day and age, it is ... 查看全文

Dumb Little Man - Tips for Life

04-20
The Causes of Stress and How to Overcome Them
Written on 4/19/2008 by David B. Bohl, the author of Slow Down Fast.Stress is one of the greatest causes of illness in our society today. It is possible that as much as 70% of all trips to the doctor ... 查看全文

Socyberty

04-06
Learning Essential Helping Skills: Crisis Telephone Listening
Rape crisis line. Can I help you?The voice is tentative. According to my training, I ask her if she is in a safe place to talk. She is. I invite her to tell me what is going on. I listen to her story,... 查看全文

Dumb Little Man - Tips for Life

05-01
Experience Required:8 Ways to Get Experience for that Job Listing
For the new job seeker or prospective career-changer, the idea of getting hired in a new field can be daunting. You’re looking for a job listing like this: “Exciting company seeks enthusiastic entry-l... 查看全文

Dumb Little Man - Tips for Life

05-01
Beating Procrastination is Easy – Once You Get Moving
Written on 4/28/2008 by David B. Bohl, the author of Slow Down Fast.One of the biggest difficulties for most people to overcome is procrastination. We all have things we dread doing, and it is so much... 查看全文
More Articles
Elanso is a professional online platform which provides translation service for corporate or individule clients, opportunities for translation practice and translation jobs, and translation tool/software-download. Our online translators provide about 186 languages' translation service, including Japanese,Korean, French, German, Spanish, etc, among which, 20,000 are English translators. And some big translation service companies in Shanghai, Beijing, Nanjing also registered here.