Custom built link_to display text problem


#1

Hi,

I want to display the number of comments for a post in a link_to. So
the link will look like below.

Comments(3)

How do i build this string in the link_to. I get an error about trying
to convert a fixnum to a string see code below.

<% case post.comments.size
when 0 %>

No Comments


<% else %>

<%= link_to ‘Comments(’ + post.comments.size + ‘)’, :action
=> ‘show’, :id => post %>


<% end %>

On another note. Would it be better to seperate this code into a
controller returning a string saying No Comments/Comments(x) incase i
needed it in more than 1 place?


#2

<…>

I want to display the number of comments for a post in a link_to. So
the link will look like below.
<…>
How do i build this string in the link_to. I get an error about trying
to convert a fixnum to a string see code below.

<% case post.comments.size
when 0 %>

No Comments


<% else %>

<%= link_to ‘Comments(’ + post.comments.size + ‘)’, :action
=> ‘show’, :id => post %>


<% end %>

How about this:

<%= link_to (post.comments.size> 0)?"Comments(#{post.comments.size})":'No comments', :action => 'show', :id => post %>

Regards,
Rimantas

http://rimantas.com/


#3

post.comments.size.to_s? :slight_smile: