Creating A Tag Cloud In Rails

Posted: July 25, 2008 in Ruby on Rails
Tags: , , ,

I had to create a tag cloud for one of my project. I did a good search
in google and got an idea about the same. But for me the challenge
was to implement it in Rails m-v-c architecture.

And none of the current articles in the web helped me much. I wanted
to make it simple and efficient. So here’s the process I followed to do it.
Thanks to Maku and Pragmatic Rails book that helped me to get
off the ground.

A tag cloud is a collection of tags (you already have in the database or
the some contents of the web page). All you want is to represent them
in a visual depiction.

In my case, I already had a list of tags in the database. I just needed to
call them and display them.

In Rails in model-view-controller architecture.

  • Model represents data. So, a table in the database is nothing but a class in model.
  • View is responsible for creating either all or part of a page.
  • Controller supplies data to the view and receives events from pages generated by the view.

Nothing comes directly from model to view. Always data comes to the
view through controller and vice-versa.

# Find the list of tags in the action of a controller.
(let’s say this action name is tag)

# Count the number of times each tag is available in the database.

# Create a hash and store both the tag_names and tag_count.

# Pass the hash to view. (tag.rhtml)

# Display each tags and resize the tags as per as their frequency.

[The more a tag is repeated in the database the bigger it will be in the
page. Use some kind of stylesheet syntax to do the same. You can apply
additional styles for colors. You can write a short function in the
app/helper and call it in view to display the tags.]


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s