OK, this is a bit of a newbie question. While I’m just stunned at the
power of rails, how does ruby do with text transformations.
Specifically, i need to detect the last item in a text list and NOT put
in the trailing commas (whereas the others do it automatically - yes
it’s for tagging).
Can anyone point me in the right direction?
From my view:
<% for tag in tags -%>
<%= tag.name %>,
<% end -%>
OK, this is a bit of a newbie question. While I’m just stunned at the
power of rails, how does ruby do with text transformations.
Specifically, i need to detect the last item in a text list and NOT put
in the trailing commas (whereas the others do it automatically - yes
it’s for tagging).
Can anyone point me in the right direction?
From my view:
<% for tag in tags -%>
<%= tag.name %>,
<% end -%>
Thanks so much in advance!
Mike
You want to use Array#join. This will insert some text between array
elements and return a string.
You might want to do this in a helper and then just call that helper
from your view. It would be reusable and would help keep your views
nice and tidy.
For example, place the following in application_helper.rb:
def tags
tags.collect { |t| “#{t.name}” }.join(', ')
end