Topic: HOWTO: Get started with acts_as_taggable_on_steroids
This is just a basic example of how to get started with this plugin that lets you easily implement tagging into your Rails app:
ruby script/plugin install http://svn.viney.net.nz/rails/plugins/a n_steroids
I will also be paginating the tags, so install the will_paginate plugin:
svn://errtheblog.com/svn/plugins/will_paginate
To start with you need to add the line 'acts_as_taggable' to the Model that you want to add tags to eg:
Class Movie
acts_as_taggable
end
Then, add the tags text_field within the form fields of the relevant form (i am being RESTful here) eg...
movies/new.rhtml:
<% form_for(:movie, :url => movies_path) do |f| %>
Title: <%= f.text_field :title %>
Description: <%= f.text_area :description %>
Tags (seperate with comma ','): <%= f.text_field :tag_list %>
<%= submit_tag "Create" %>
<% end %>
Create your Tags Controller:
class TagsController
def index
@tags = Tag.find(:all)
enddef show
@tag = Tag.find(params[:id])
@movies = Movie.paginate_by_id(Movie.find_tagged_with(@tag), :page => params[:page], :per_page => 3)
end
end
tags/index.rhtml:
TAG List...
<% @tags.each do |t| %>
<%= link_to t.name.capitalize, tag_path(t) %>
<% end %>
tags/show.rhtml:
TAG: <%= @tag.name.capitalize %>
<% @movies.each do |m| %>
<%= link_to m.title, movie_path(m.id) %>
<%= m.description %>
<% end %>
<%= will_paginate @movies %>
This will basically let you add tags to new 'movies', view a list of all tags, and then also see a list of all 'movies' tagged with a particular tag - and this list is paginated. Of course you can also implement Tag clouds, checkout the plugins readme file for more info