# String concatenation but with a minor twist

**URL:** <https://www.ruby-forum.com/t/string-concatenation-but-with-a-minor-twist/124033>\
**Category:** Rails\
**Created:** [December 23, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/string-concatenation-but-with-a-minor-twist/124033 "2007-12-23T20:26:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [December 23, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/string-concatenation-but-with-a-minor-twist/124033/1 "2007-12-23T20:26:02Z")

</div>

Hello,

I’m just starting out with RoR and have been playing around with making  
my own blog using RoR as a quick way of getting my feet wet. RoR is neat  
isn’t it! Liking the link\_to etc helpers, seems such a good way of doing  
things.

Anyway, my little problem goes like this:

I want a link that says Comments (#of comments), for instance “Comments  
(2)”.

To make my link without the number, I have used:

\<%= link\_to ‘Comments’, {:action =\> ‘show’, :id =\> post, :anchor =\>  
‘comments’}, :class =\> ‘comments’ %\>

And I know that:

\<%= post.comments.count %\>

tells me how many comments are for this post. (I’m looping through a  
load, so the :id exists etc.)

How do I do this?

\<%= link\_to ‘Comments (post.comments.count)’, {:action =\> ‘show’, :id =\>  
post, :anchor =\> ‘comments’}, :class =\> ‘comments’ %\>

so that the code actually executes? I can’t work it out!

Thanks!

---

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [December 23, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/string-concatenation-but-with-a-minor-twist/124033/2 "2007-12-23T20:30:27Z")

</div>

On 23 Dec 2007, at 19:26, Richard Brashaw wrote:

> How do I do this?
> 
> \<%= link\_to ‘Comments (post.comments.count)’, {:action =\>  
> ‘show’, :id =\>  
> post, :anchor =\> ‘comments’}, :class =\> ‘comments’ %\>

If you have a string delimited with double quotes (or equivalent) you  
can embed bits of ruby with #{}, for example  
“Hello #{person.name}”

Fred

---

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [December 23, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/string-concatenation-but-with-a-minor-twist/124033/3 "2007-12-23T20:32:25Z")

</div>

Richard Brashaw wrote:

> How do I do this?
> 
> \<%= link\_to ‘Comments (post.comments.count)’, {:action =\> ‘show’, :id =\>  
> post, :anchor =\> ‘comments’}, :class =\> ‘comments’ %\>
> 
> so that the code actually executes? I can’t work it out!
> 
> Thanks!

For all ruby code, you can execute ruby is a string like so:

“Comments #{post.comments.count}” #=\> Comments 3

Note the double quotes, single quote string will not allow this. You  
can also just use +, alhtough #{} is generally cleaner.

'Comments ’ + post.comments.count.to\_s

---

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [December 23, 2007, 8:34pm UTC](https://www.ruby-forum.com/t/string-concatenation-but-with-a-minor-twist/124033/4 "2007-12-23T20:34:06Z")

</div>

Frederick C. wrote:

> On 23 Dec 2007, at 19:26, Richard Brashaw wrote:
> 
> > How do I do this?
> > 
> > \<%= link\_to ‘Comments (post.comments.count)’, {:action =\>  
> > ‘show’, :id =\>  
> > post, :anchor =\> ‘comments’}, :class =\> ‘comments’ %\>
> 
> If you have a string delimited with double quotes (or equivalent) you  
> can embed bits of ruby with #{}, for example  
> “Hello #{person.name}”
> 
> Fred

Thank you so much! I remember seeing that before somewhere now!
