Forum: Ruby on Rails Custom built link_to display text problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John Butler (Guest)
on 2006-04-02 12:07
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 %>
        <p>No Comments</p>
    <% else %>
        <P><%= link_to 'Comments(' + post.comments.size + ')', :action
=> 'show', :id => post %></P>
    <% 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?
Rimantas Liubertas (Guest)
on 2006-04-02 13:39
(Received via mailing list)
<...>
> 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 %>
>         <p>No Comments</p>
>     <% else %>
>         <P><%= link_to 'Comments(' + post.comments.size + ')', :action
> => 'show', :id => post %></P>
>     <% end %>

How about this:

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


Regards,
Rimantas
--
http://rimantas.com/
224f363ba127f61867a0c87009c20497?d=identicon&s=25 Adam B. (admanb)
on 2006-04-02 18:51
post.comments.size.to_s? :)
This topic is locked and can not be replied to.