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 -%> <small><%= tag.name %></small>, <% end -%> Thanks so much in advance! Mike -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m myself new to Ruby and Rails, but I would probably try something like this: <small><%= (tags.collect {|t| t.name}).join("</small>, <small>") %></small> Regards, Thomas 2007/1/15, Mike Dershowitz <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > 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 -%> > <small><%= tag.name %></small>, > <% end -%> > > Thanks so much in advance! > > Mike > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mike Dershowitz wrote:> From my view: > <% for tag in tags -%> > <small><%= tag.name %></small>, > <% end -%>Try: <% for tag in tags -%> <small><%= tag.name %></small><% if tag != tags.last %>,<% end %> <% end -%> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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| "<small>#{t.name}</small>" }.join('', '') end V/r Anthony Eden On 1/15/07, David <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Mike Dershowitz wrote: > > From my view: > > <% for tag in tags -%> > > <small><%= tag.name %></small>, > > <% end -%> > > Try: > > <% for tag in tags -%> > <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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mike Dershowitz 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 -%> > <small><%= tag.name %></small>, > <% end -%> > > Thanks so much in advance! > > MikeYou want to use Array#join. This will insert some text between array elements and return a string. <%= tags.collect { |tag| "<small>#{tag.name}</small>" }.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) %> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alex Wayne 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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" <small><%= tags.to_sentence %></small> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---