Forum: Ruby on Rails Tag Cloud Code

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.
Alex (Guest)
on 2006-03-19 14:58
Does anyone have any sample code for a tag cloud.
I've looked at typo but they've just got complicated sql (which I don't
have a clue about). All I want is to select tags and make them
bigger/smaller depending on the number of pages that are 'tagged' by
them. Thanks in advance
Joey (Guest)
on 2006-03-19 15:13
(Received via mailing list)
On 3/19/06, Alex <removed_email_address@domain.invalid> wrote:
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails


In the controller:
@tags = Tag.find(:all,:include => :tagged_objects)

In  the view:
<% @tags.each do |t| %>
  <%= link_to(tag.name,{}, {:style => "font-size: #{(
tag.tagged_objects.size+1)*10}px"}) %>
<% end %>
Or something like that.

joey__
http://www.feedreed.com
tijer (Guest)
on 2006-03-19 15:28
(Received via mailing list)
I've posted a ruby-sample-code for tagclouds on my blog, a while ago
Alex (Guest)
on 2006-03-19 15:38
tijer wrote:
> I've posted a ruby-sample-code for tagclouds on my blog, a while ago

What's the address?
Anthony Rudgick (Guest)
on 2006-03-19 18:12
i'll assume he's the author of
http://blog.craz8.com/articles/2005/10/28/

Alex wrote:
> tijer wrote:
>> I've posted a ruby-sample-code for tagclouds on my blog, a while ago
>
> What's the address?
vincew (Guest)
on 2006-03-20 07:45
Would be interested to know what the database structure is for a tag
cloud?  I saw this post:
http://www.pui.ch/phred/archives/2005/04/tags-data...

but apparently there may be a different recommended path with RoR?  Any
insights?

VW
albert elegance (Guest)
on 2006-03-20 08:47
(Received via mailing list)
My tags table caches the number of posts they're associated with as a
database field, named post_count. The code I use (in a helper):

def cloud_view(tags)
  html = ""
  mid = tags.map {|i| i.post_count}.max / 3.0

  tags.each do |tag|
    size = 100 * tag.post_count / mid
    size = 80 if size < 80
    html << link_to(tag.name, "post/list/#{tag}", :style =>
"font-size: #{size}%") << " "
  end

  return html
end

This caps the maximum size of a tag to 300%, and the minimum to 80%.
This topic is locked and can not be replied to.