reback
December 23, 2007, 8:26pm
1
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!
reback
December 23, 2007, 8:30pm
2
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
reback
December 23, 2007, 8:32pm
3
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
reback
December 23, 2007, 8:34pm
4
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!