New Rails Plugin - Acts As Noteable

Posted by admin 10/06/2007 at 13h51

So I was at work and I needed to add notes to multiple models in the same project (before i only had to worry about one model with notes, so it was pretty straightforward). So i bit down and knocked out this little plugin.

I borrowed heavily from Acts As Rateable by Juixe


Run the following command:

script/plugin install

The install will try and copy some scripts over, this works sometimes. If you get a rake error do the following:

  1. cd vendor/plugins/acts_as_noteable
  2. rake update_scripts

Create a new rails migration:

script/generate migration add_notes_to_project

and add the following self.up and self.down methods

    def self.up
        create_table "notes", :force => true do |t|
            t.column "from", :string, :limit => 50, :default => "" 
            t.column "body", :text, :default => "" 
            t.column "created_at", :datetime, :null => false 
            t.column "noteable_id", :integer, :default => 0, :null => false 
            t.column "noteable_type", :string, :limit => 15, :default => "", :null => false 

    def self.down
        drop_table :notes 

(one day i’ll add a migration script to the plugin to automatically add this :) )

After that run “rake migrate” to create the table.

Now all you need to do is call acts_as_noteable in your model:

class Model < ActiveRecord::Base

Now your model is notable, but that’s not all.

For only 19.95 you get a Helper method to help you create a nice form. Ok, so i am not actually charging, but whatever.

Here’s how the helper works.

In the view you want to display notes in put the following:

<%= display_notes(object) %>

This will create a div filled with the note entries for the object and a AJAX form to add new notes to the object as well as delete notes from the object. The add and delete methods are stored in the notes_controller.rb that gets copied over into your app/controllers directory.

The full implementation for this method is:

<%= display_notes(obj, partial_name = "notes/note", controller_name = "notes") %>

I added the partial name and controller name in case you have multiple controllers in different folders, this way you can display notes for objects whose controllers are buried deep in a folder.

Example: I have an “admin” folder for all the backend stuff and I needed a way to reference the notes controller from different places, otherwise RAILS will assume you are trying to reference a controller from the current folder your in.

After you have called display_notes, your done.

Note** This plugin has not been tested with Rails 2.0 yet



This entry was posted on 10/06/2007 at 13h51 and Posted in . You can follow any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags , , ,


Use the following link to trackback from your own site:


Leave a comment