Quick Start

This quick start guide will get you up and running with the Tagging plugin in just a few minutes.

In the following examples, we’ll be adding tagging capabilities to a fictions Article model. We’ll then be looking at how to add a tag editor to your views, and how to display tag clouds.

Before we begin though, let’s get the Tagging plugin installed.

Installing the Plugin

You would normally use the script/plugin utility that comes with Rails to install a plugin. However, that script assumes a repository layout that is different than the one used for the Tagging plugin.

Therefore, you can export the plugin from its Subversion repository, check it out, or add it as a svn:externals.

svn export http://pmade.com/svn/oss/tagging/branches/rel/1.0/ vendor/plugins/tagging

Once you have the Tagging plugin installed in your vendor/plugins directory, you’ll want to generate the migration for the database changes, and the default CSS file for the tag cloud:

script/generate tagging add_tagging
rake db:migrate

Add Tagging To Your Model

Preparing your model classes for tagging is straight forward:

1
2
3
 class Article < ActiveRecord::Base
   acts_as_taggable
 end

Controller Actions

The Tagging plugin includes some controller actions to handle tag editing. To use the built-in tag editor, use the tagging_helper_for class method in your controller:

1
2
3
 class ArticlesController < ApplicationController
   tagging_helper_for Article
 end

Using Tagging in Your Views

Once you have the tagging controller actions in place, creating a tag editor and tag cloud is simple:

1
2
 <h1>Tags: <%= tag_editor_for(@article) %></h1>
 <%= tag_cloud_for(@article) %>

As mentioned earlier, the Tagging plugin comes with a default CSS file for tag clouds. You can include this CSS file in your view layout file along with your other CSS files:


<%= stylesheet_link_tag('tagging') %>

Manual Tag Manipulation

If you wanted to write you own tag editor, or otherwise manipulate tags outside the built-in controller actions, you can access the tags directly using the tags association:

1
2
 article.tags.add("ruby pmade")
 article.tags.remove("python")

Read the Code

If you want to move beyond the default tag cloud or tagging behavior, the best place to turn is the source code. It is well documented and has good tips for using different features of the plugin.

Below is a listing of the source code files in the Tagging lib directory, and a brief description.

Source File Description
extend_ar.rb Methods and associations that get mixed in with your model
extend_ac.rb Contains the methods (actions) that get added to your controller
extend_av.rb This is where the tag cloud and tag editor generators live
tag.rb The Tag model for representing a unique tag in your database
tagging.rb The Tagging model to link an instance of a model to a Tag
cloud.rb Helper methods for generating tag clouds

Updated Feb 22, 2007 by Peter Jones

Tags:

This page hasn't been tagged yet.

Comments:

Have something to say? Login to post a comment.