Acts As Taggable on Steroids Ruby On Rails Plugin With Paginate
Acts As Taggable On Steroids is a great plugin for Rails that makes adding tags to your application quick and easy. Check it out on github here => http://github.com/mattetti/acts_as_taggable_on_steroids/tree/master
I threw this post together with some notes that I took along the way when setting up the plugin for use.
Installation:
script/plugin install http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids |
Setup
Setup your migration by running the below:
ruby script/generate acts_as_taggable_migration
rake db:migrate
Add the acts_as_taggable to your model, in my case its the Events model…
class Event < ActiveRecord::Base # acts as taggable on roids acts_as_taggable |
Now I needed to add a way to allow tags to be added to Events, in my form for my Event I added the following:
<p> <%= f.label 'Tag List (Delimiter = ,)' %><br /> <%= f.text_field :tag_list %> </p> |
To view the tags associated this a Event on the Show page, along with linking to the actual tag to search for other events tagged with the same item, I added the following:
<p><strong>This event was tagged with</strong>: <% for tag in @event.tags %> <%= link_to tag.name, events_path(:view =>'tag', :tag => tag.name) %><% end %> </p> |
Tag Clouds
In order to use the plugin’s build in tag cloud functionality you need to add the helper to your application helper by doing the following:
module ApplicationHelper include TagsHelper end |
In your controller where you are planning on using your tag cloud add the below, this will grab the counts of all your tags so the helper can generate your cloud. Again in my case I am putting this in my Events controller.
@tags = Event.tag_counts |
Now you use the following where you wish to show your views:
<% tag_cloud @tags, %w(tag1 tag2 tag3 tag4) do |tag, css_class| %> <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %> <% end %> |
In your main CSS file you should add something to match the tags we specified above, this sets your font sizes for the cloud display generated by the helper…
.tag1 { font-size: 1.0em; } .tag2 { font-size: 1.6em; } .tag3 { font-size: 2.7em; } .tag4 { font-size: 3.8em; } |
Tag Clouds with Paginate Example
Here I am passing the page and tag to my events model to find all events tagged with that tag, and then use the results of that query and passing it onto paginate.
def self.tag_event_list(page, tag) options = Event.find_options_for_find_tagged_with(tag).merge :page => page, :per_page => 10, :order => 'date DESC' paginate(options) end |
Other Notes:
I added the following to my environment.rb file so that the unused tags get destroyed if they are no longer in use by any events.
# automatically remove dead tags Tag.destroy_unused = true |
For more help check out the read me here: http://github.com/mattetti/acts_as_taggable_on_steroids/tree/master
Andrew
Hey there, I love your tutorials – very clear and easy to understand. However, when I click on the links, it’s returning the entire index for me (i.e. even things that are not tagged at all). I’m sure it’s just something I missed. My model is ‘restaurants’ and the code I put in my view is:
modified from yours, of course.
Does it look right, or did I miss a step?
Thanks again!!!
Andrew
d’oh – it wouldn’t let me post the code snippet from Gist. Let me try to submit it as plain text…
This event was tagged with:
‘tag’, :tag => tag.name) %>
Thanks!
Andrew
Okay, that didn’t work out – here’s a link to the code which should work: http://gist.github.com/17899
Ben
I ran into a problem with the options/paginate(options). The ‘count’ phase of the process added “DISTINCT(model_name.*)” inside the count, which freaked out both Sqlite and MySQL. I had to resort to paginate_by_sql to end-around the problem. Very bad…