Reputation: 11778
I'm trying to create a helper to display rating stars based on a model attribute.
I have the following:
def show_profile_stars(profile)
content_tag :span, :class => 'stars' do
profile.stars.times do
image_tag("stars.gif", :size => "30x30", :class => "gold")
end
end
end
'stars' is an integer field.
But it's not rendering the image tags, instead it just displays the 'stars' number literally.
If I put just image_tag without the iteration block it does show the image, the problem is with the iteration.
I guess I'm missing something about methods that receive blocks (I'm still new at RoR).
Any help?
Thanks!
Upvotes: 0
Views: 954
Reputation: 107718
Use the concat
helper:
def show_profile_stars(profile)
content_tag :span, :class => 'stars' do
profile.stars.times do
concat(image_tag("stars.gif", :size => "30x30", :class => "gold"))
end
nil
end
end
You also need to return nil
at the end of the content_tag
so that it doesn't output stars
.
Upvotes: 2
Reputation: 9093
Two things, wouldn't this be done in CSS using a class on the span (e.g. one-star, two-star, etc)?
Anyway, to actually do what you want to do try:
stars = []
profile.stars.times { stars << image_tag("stars.gif", :size => "30x30", :class => "gold") }
content_tag :span, stars.join("\n").html_safe, :class => "stars"
Upvotes: 1