I have an array, which is loaded from a database. It is in the following format:
["aaa:bbb", "ccc:ddd"]
And need to build an HTML definition list from the array:
<dl>
<dt>aa</dt>
<dd>bb</dd>
<dt>cc</dt>
<dd>dd</dd>
</dl>
Looks easy, isn't it?
I wanted to create a helper, which would go through the array, split the items on the :
's, and enclose them in tags:
<%= build_def_list(array) %>
However, I have encountered a problem. Namely, when I call a Rails helper from a view, all of its content goes to the output. That is, when I declare a variable within the body of a function and assign a value to it, the variable instantly goes to the output. Is there a way to suppress the printing of everything inside the body of a function?
UPD
Seriously, how to make this with helper?..
<dl>
<% deflist.each do |item| %>
<dt><%= item.split(':').at(0) %>:</dt>
<dd><%= item.split(':').at(1) %></dd>
<% end %>
</dl>
Try this for your helper:
Then in your view call
<%= build_def_list(array) %>
Addition: (To filter what to print)
Thanks to @Arup who reminded me the additional check:
To print only specific items, you can filter your input array first deleting any elements you need to remove then use the resultant array to build the
content_tag
s as:This will first split the elements of the
array
by:
and delete all the elements if they are found inreject_keys
array. The result is then used to build thecontent_tag
s.