Hello there, I do not know if my topic is right so I''ll explain the problem : I have a Project model : class Project < ActiveRecord::Base has_and_belongs_to_many :services end In my views. I would like to list the services in the title of the page. I did : <% for service in @project.services %-><%= service.name_fr.downcase -%>, <% end -%>. The problem is with the listing itself, it puts this : {Project.name} required {service.name1}, {service.name2}, {service.name3},. There is a problem with the last comma. It looks awful from a web developer view. I would like : {Project.name} required {service.name1}, {service.name2} and {service.name3}. Notice the attribute «and» and «dot» at the end How do you call that thing I am looking for? Is there a way to do it like the second exemple? Thx a lot for your time and help. Hugues -- Posted via http://www.ruby-forum.com/.
On Sun, Sep 6, 2009 at 3:33 PM, Hugues Brunelle<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I did : > <% for service in @project.services %-><%= service.name_fr.downcase -%>, > <% end -%>. > > The problem is with the listing itself, it puts this : > {Project.name} required {service.name1}, {service.name2}, > {service.name3},. > > There is a problem with the last comma. It looks awful from a web > developer view. > > I would like : > {Project.name} required {service.name1}, {service.name2} and > {service.name3}.Something like this should work: @project.services.collect {|service| service.name}.join(", ").gsub(/, ([A-Za-z]+)$/,'' and \1.'') :: though you might need to tweak that regex, depending on what''s in a service name... HTH, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan
@project.services.collect{|s| s.name}.to_sentence On Sep 6, 3:33 pm, Hugues Brunelle <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello there, > I do not know if my topic is right so I''ll explain the problem : > > I have a Project model : > > class Project < ActiveRecord::Base > has_and_belongs_to_many :services > end > > In my views. I would like to list the services in the title of the page. > > I did : > <% for service in @project.services %-><%= service.name_fr.downcase -%>, > <% end -%>. > > The problem is with the listing itself, it puts this : > {Project.name} required {service.name1}, {service.name2}, > {service.name3},. > > There is a problem with the last comma. It looks awful from a web > developer view. > > I would like : > {Project.name} required {service.name1}, {service.name2} and > {service.name3}. > > Notice the attribute «and» and «dot» at the end > > How do you call that thing I am looking for? > Is there a way to do it like the second exemple? > > Thx a lot for your time and help. > > Hugues > -- > Posted viahttp://www.ruby-forum.com/.
On Sun, Sep 6, 2009 at 5:47 PM, Eric<ericghill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > @project.services.collect{|s| s.name}.to_sentenceSweet, didn''t know that one. Let''s add the period, though: @project.services.collect{|s| s.name}.to_sentence.concat(".") Et voila, another good day''s work :-) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan