# When to Use a Helper Method in Rails

DevFeed: [When to Use a Helper Method in Rails](<https://devfeed.tech/articles/when-to-use-a-helper-method-in-rails-28265.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/rails/2020/03/04/when-to-use-a-helper-method-in-rails.html>)

Author: Fuzzygroup

Published: 2020-03-04T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Code](<https://devfeed.tech/topics/code.md>), [HTML](<https://devfeed.tech/topics/html.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [data-structure](<https://devfeed.tech/tags/data-structure.md>), [function](<https://devfeed.tech/tags/function.md>), [html](<https://devfeed.tech/tags/html.md>), [netlabeler](<https://devfeed.tech/tags/netlabeler.md>), [rails](<https://devfeed.tech/tags/rails.md>), [rake](<https://devfeed.tech/tags/rake.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [view](<https://devfeed.tech/tags/view.md>)

## AI overview

This Rails tutorial explains when to use a helper method to transform stored hash data into HTML for display. It presents a testable display_labeled_data helper that builds paragraph output, converts newlines to HTML breaks, joins the result, and marks it html_safe. It also notes that Rails helpers share a global namespace.

## Source excerpt

One of the more interesting facilities in Rails helpers which reside in app/helpers/*.rb and are used, well, as helpers. Helpers are basically display level execution of Ruby code. I recently wrote what I thought was a great example of a helper so I thought I'd document it. What I had was a object, stored in the database which had a bunch of data that I needed to display: the object, called a label A serialized hash called labeled_data label0 label1 label2 label3 label4 label5 label6 label7 label8 label9 What I wanted to do was transform this, at display time, to an HTML structure like this: <p>label0: <b>The Data for Label 0</b> <p>label1: <b>The Data for Label 0</b> and, correctly, not show label2, etc if they were blank. And, naturally, do the right thing when there was no data in the labeled_data hash, etc. And while I could have done this with a view and code wrapped in evaluation / output tags: <% and %> or <%= or %> it is dramatically cleaner to write this type of thing as a helper and then call it as a function like display_labeled_data(label). Not only does this make the code much cleaner than evaluation / output tags, it also has the advantage of being testable. Now all that said, here is the code: def display_labeled_data(label) return if label.labeled_data.nil? return if label.labeled_data.keys.nil? output = [] label.labeled_data.keys.each do |key| output << "<p>" output << key output << ":" tmp = label.extracted_data[key] if tmp tmp = tmp.gsub(/\n/,'
') end output << tmp output << "</p>" end output.join("\n").html_safe end The key idea here is: create an output object, in this case an array or [] loop a data structure - label.labeled_data.keys as you loop the data structure, put things into the output object like a paragraph tag, the name of the key i.e. "label0", a ':' clean up the data by converting new lines to html line breaks add the data into the output element add a closing p tag add a line break onto every element of the output object (using