I am on Rails 4 trying to build a simple helper to reduce some of the code in my view.
Here is the view code (show.html.erb) before using a helper:
<% unless @article.long_effects.blank? %>
<ul>
<% @article.long_effects.split(';').each do |effect| %>
<li><%= effect %></li>
<% end %>
</ul>
<% end %>
and here is the helper I built for the above code:
def list(attribute)
unless attribute.blank?
content_tag(:ul) do
attribute.split(';').each do |a|
content_tag(:li, a)
end
end
end
end
which I then call from the view like so
<%= list(@article.long_effects) %>
Unfortunately, the helper is not returning anything. This is my first time writing a helper that returns HTML, so maybe I am doing something wrong?
from
def list(attribute)
unless attribute.blank?
content_tag(:ul) do
attribute.split(';').each do |a|
content_tag(:li, a)
end
end
end
end
to
def list(attribute)
unless attribute.blank?
content_tag(:ul) do
attribute.split(';').each do |a|
concat content_tag(:li, a)
end
end
end
end
concat
method will be useful to join the collection object from looping conditions.