# refactoring a helper method

**URL:** <https://discuss.rubyonrails.org/t/refactoring-a-helper-method/37839>\
**Category:** rubyonrails-talk\
**Created:** [December 31, 2008, 1:56pm UTC](https://discuss.rubyonrails.org/t/refactoring-a-helper-method/37839 "2008-12-31T13:56:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [December 31, 2008, 1:56pm UTC](https://discuss.rubyonrails.org/t/refactoring-a-helper-method/37839/1 "2008-12-31T13:56:55Z")

</div>

I am working on a site that does web surveys. I have created a helper method to create a likert scale table (e.g., 5 columns + row title, strongly dislike \<-\> strongly like). This method accepts an array of hashes and then uses the built in Rails helper form.radio\_button to unwrap the hash and build the table. The function is below:

&nbsp;&nbsp;# procedurally goes into an array of hashes and generates a likert table &nbsp;&nbsp;def likert\_table(items = , f=f) &nbsp;&nbsp;&nbsp;&nbsp;result = "" # collector string &nbsp;&nbsp;&nbsp;&nbsp;items.each {|item| item.each {|attribute,text| result = result + likert\_row(attribute,text,f)}} &nbsp;&nbsp;&nbsp;&nbsp;return "\<table class='likert\_table'\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr class='likert\_table\_header\_row'\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<th\>\</th\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<th\>Strongly Dislike\</th\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<th\>Dislike\</th\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<th\>Neutral\</th\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<th\>Like\</th\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<th\>Strongly Like\</th\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{result} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</table\>" &nbsp;&nbsp;end

&nbsp;&nbsp;# create a row in a table with the 5 selectable radio buttons &nbsp;&nbsp;def likert\_row(attribute, text, f=f) &nbsp;&nbsp;&nbsp;&nbsp;"\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td align='right' class='likert\_row\_label'\>#{f.label attribute, text}\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td align='center' class='likert\_row'\>#{f.radio\_button attribute, 1}\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td align='center' class='likert\_row'\>#{f.radio\_button attribute, 2}\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td align='center' class='likert\_row'\>#{f.radio\_button attribute, 3}\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td align='center' class='likert\_row'\>#{f.radio\_button attribute, 4}\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td align='center' class='likert\_row'\>#{f.radio\_button attribute, 5}\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\</tr\>" &nbsp;&nbsp;end

However, I had a few questions about refactoring.

First, do I need to pass "f" in both of those functions? I use it in the f.radio\_button helper (as this method would be called inside a form\_for) but it seems clunky to have to add "f" to the arguments every time.

Second, is using a string variable to collect the output of the iterations and then returning that string the best way to construct an iterative helper?

Third, is there a simpler way to iterate through this than an array of hashes? I can build a likert table like so:

&nbsp;&nbsp;&nbsp;&nbsp;\<%= likert\_table([:rates\_fighting =\> "Fighting", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_platform =\> "Platform", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_shooter =\> "Shooter", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_action\_adventure =\> "Action Adventure", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_system\_sim =\> "System Simulator", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_life\_sim =\> "Life Simulation", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_rpg =\> "Role Playing Game", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_mmorpg =\> "Online Role Playing Game", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_strategy =\> "Strategy", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_vehicle\_sim =\> "Vehicle Simulation", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_music =\> "Music", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_puzzle =\> "Puzzle", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_sports =\> "Sports", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:rates\_casual =\> "Casual"], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f)%\>

...but it feels un-Railslike, especially with that dangling "f". Later I will be pulling these values from a database and using a collection instance variable. As a beginner ruby programmer I have hit my ceiling on this one. Any thoughts? Thanks in advance.
