Link_to - naming issue

Hey all, I’m trying to get a link_to to display a link to the number
of comments for a post.

I can get the number + text with

pluralize post.comments.count, ‘Comment’

but I can’t seem to do

<%= link_to pluralize post.comments.count, ‘Comment’, :action =>
‘show’, :id => post, :anchor => ‘comments’ %>

What would be the best way to do this?

This is what you are after:

<% link_text = “#{post.comments.count} #{pluralize(post.comments.count,
‘Comment’)}” -%>
<%= link_to link_text, :action => ‘show’, :id => post, :anchor =>
‘comments’ %>

If you nest function calls, you need to parenthesize the inner calls.

Brien wrote:

‘show’, :id => post, :anchor => ‘comments’ %>

What would be the best way to do this?


Sincerely,

William P.

http://www.billpratt.net
[email protected]

Hi –

On Sat, 18 Aug 2007, Brien wrote:

<%= link_to pluralize post.comments.count, ‘Comment’, :action =>
‘show’, :id => post, :anchor => ‘comments’ %>

What would be the best way to do this?

Putting the arguments to pluralize in parentheses should do it:

<%= link_to pluralize(post.comments.count, ‘Comment’), :action =>
‘show’, :id => post, :anchor => ‘comments’ %>

David

Hi –

On Sat, 18 Aug 2007, William P. wrote:

This is what you are after:

<% link_text = “#{post.comments.count} #{pluralize(post.comments.count,
‘Comment’)}” -%>

That’s going to give you the number twice (10 10 Comments), since
pluralize already puts the number in. You just need to put the
arguments to pluralize in parentheses (see my other post).

David

Correct, sorry 'bout that.

[email protected] wrote:

That’s going to give you the number twice (10 10 Comments), since
pluralize already puts the number in. You just need to put the
arguments to pluralize in parentheses (see my other post).

David


Sincerely,

William P.

http://www.billpratt.net
[email protected]