Eliminating trailing commas

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

Mike D. wrote:

From my view:
<% for tag in tags -%>
<%= tag.name %>
<% end -%>

Try:

<% for tag in tags -%>
<%= tag.name %><% if tag != tags.last %>,<% end
%> 
<% end -%>

Mike D. wrote:

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.

<%= tags.collect { |tag| “#{tag.name}” }.join(’, ') %>

This creates an array of tag names, with html around it, and then join
the elements with a comma and space.

A cleaner way may involve a helper though.

def tag_list(tags)
names = tags.collect do |tag|
content_tag(‘small’, tag.name)
end
names.join(’, ')
end

Then instead of that big ugly RHTML tag there, you just have:

<%= tag_list(tags) %>

Alex W. wrote:

A cleaner way may involve a helper though.

def tag_list(tags)
names = tags.collect do |tag|
content_tag(‘small’, tag.name)
end
names.join(’, ')
end

Then instead of that big ugly RHTML tag there, you just have:

<%= tag_list(tags) %>

Slightly more clean:

def tag_list(tags)
tags.map { |tag| content_tag(‘small’, tag.name) }.join(’, ')
end

I’m myself new to Ruby and Rails, but I would probably try something
like this:

<%= (tags.collect {|t| t.name}).join(“”)
%>

Regards,
Thomas

2007/1/15, Mike D. [email protected]:

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

V/r
Anthony E.

On 1/15/07, David [email protected] wrote:

<small><%= tag.name %></small><% if tag != tags.last %>,<% end

%> 
<% end -%>


Posted via http://www.ruby-forum.com/.


Cell: 808 782-5046
Current Location: Melbourne, FL

Just to add one more…
Since you don’t appear to be linking each element,
how about to_sentence from Active Support?

tags = %w{shoes shirts socks pants}
=> [“shoes”, “shirts”, “socks”, “pants”]

tags.to_sentence
=> “shoes, shirts, socks, and pants”

<%= tags.to_sentence %>

Or, without the “and”

tags.to_sentence(:connector => nil)
=> “shoes, shirts, socks, pants”
Although, this inserts an extra space
before the last element for some reason.

  • Brian