1

I'm sure I saw a while back a rails helper method where .each in the view accepts a separator such as a comma.

So say I want:

- @results.each do |result| 
  = result.title

#to output
result 1, result 2, result 3

TIA

2 Answers 2

8

I think

@results.map(&:title).to_sentence

should do the job. See docu.

1
  • Awesome that's the one. Thanks!
    – mark
    Aug 14, 2010 at 11:56
3

Not sure what you are refering to, but:

@results.collect { |r| r.title }.join( ',' )

should do what you want.

2
  • Thanks for answer but to_sentence is what I was looking for. Purely pedanticism mind you. :)
    – mark
    Aug 14, 2010 at 11:58
  • I agree. to_sentence is new to me too, so I'm glad I came here. Aug 15, 2010 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.