Friday, May 8, 2009

using rails acts as taggable on plugin

by Sandip Ransing 1 comments
Tags

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.

Plugin Installation

script/plugin install git://github.com/mbleigh/acts-as-taggable-on.git

Gem Installation


gem install mbleigh-acts-as-taggable-on --source http://gems.github.com


Auto Install
include following line in environment.rb

config.gem "mbleigh-acts-as-taggable-on", :source => "http://gems.github.com", :lib => "acts-as-taggable-on"


Post Installation (Rails)

1. script/generate acts_as_taggable_on_migration
2. rake db:migrate


Usage
Add following line in your model for which you wanted to be tagged.

acts_as_taggable_on :tags

Example

class Post < ActiveRecord::Base
acts_as_taggable_on :tags
end


In your View for Post




In your controller create action

@post = Post.new (params[:post])
# Or just hardcode to test
@post.tag_list ="awesome, slick, hefty"
@post.save


What are the methods provided ??

@post.tag_list = "awesome, slick, hefty"

Post.tagged_with("awesome", :on => :tags) # => [@post1, @post2]


Post.tag_counts # => [,...]

Named Scope

class Post "created_at DESC"
end


Post.tagged_with("awesome").by_creation

Pagination can be added on list of tags using will_paginate plugin

List Of Tags .paginate(:page => params[:page], :per_page => 20)


@post.find_related_tags # => will give related posts having related tags. [ @post1, @post2, ...]



Tag Owners

class User < ActiveRecord::Base
acts_as_tagger
end



class Post < ActiveRecord::Base
acts_as_taggable_on :tags
end


@some_user.tag(@some_post, :with => "paris, normandy", :on => :tags)
@some_user.owned_taggings
@some_user.owned_tags
@some_post.tags_from(@some_user)



Cheers !
$@ndip

blog comments powered by Disqus

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter