Written by Sean Behan on Sun Jun 17th 2012

You can download it here http://github.com/suitmymind/acts-as-taggable-on-steroids as well as read usage info (which is for the most part reprinted here).

./script/plugin install http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids
./script/generate acts_as_taggable_migration
rake db:migrate
Then in your model
class Post < ActiveRecord::Base
    acts_as_taggable
 end
And usage is as follows
p = Post.find(:first)
p.tag_list # []
p.tag_list = "Funny, Silly"
p.save
p.tag_list # ["Funny", "Silly"]
p.tag_list.add("Great", "Awful")
p.tag_list.remove("Funny")

to find...

Post.find_tagged_with('Funny, Silly') Post.find_tagged_with('Funny, Silly', :match_all => true)

To use this in a form and let users enter a comma separated list of tag names...

form_for @post do |f|
f.text_field :tag_list

And to get a tag cloud

  #controller
  class PostController < ApplicationController
    def tag_cloud
      @tags = Post.tag_counts
    end
  end

and in view...

<style> .css1 { font-size: 1.0em; } .css2 { font-size: 1.2em; } .css3 { font-size: 1.4em; } .css4 { font-size: 1.6em; } </style>

<% tag_cloud @tags, %w(css1 css2 css3 css4) do |tag, css_class| %> <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %> <% end %>

*Note. If you have a controller "tags_controller.rb" and the auto generated (if you used ./script/generate) helper file "tags_helper.rb" you'll need to make sure to copy the contents of the plugin lib module of the same name, into the helper file. You'll get an error otherwise.


Tagged with..
#active record #migration #model #plugins #tags #Ruby on Rails

Just finishing up brewing up some fresh ground comments...