Create a Simple Ruby on Rails Plugin in Minutes
July 20, 2007 – 5:42 pmI am working on a class (when I take a break developing for ThemBid.com) that allows easy usage of the Digg API within Ruby on Rails. I wanted to integrate the class in such a way that I can easily share the code within my own projects and eventually for public usage. I decided to go with making a simple plugin.
The other option is to put this code in the /lib directory and share the class in that manner, but I wanted to do some dabbling with plugins and set the stage for releasing this code publicly. Following are the methods I used to create the plugin. If you are starting with Ruby on Rails this method can help you get started quickly.
Creating the Plugin
script/generate plugin [name of your plugin in lowercase with underscores for spaces]
Edit the following file:
vendor/plugins/[name of your plugin in lowercase with underscores for spaces]/lib/[name of your plugin].rb
and add your code. Use the module keyword instead of class and use the CamelCase convention to name the module. Then within that module definition you can define your classes.
Edit the following file:
vendor/plugins/[name of your plugin in lowercase with underscores for spaces]/init.rb
and then add:
require '[name of your plugin in lowercase with underscores for spaces]'
Using the Plugin
In the controller were you want to use the module put:
include [name of your plugin in CamelCase]
before the class definition. If you want the module to be accessible from any controller, put the include in this file:
app/controllers/application.rb
You can instantiate your class like so :
[name of your plugin in CamelCase]::[name of class].new
Not that when you are debugging your plugin you will need to restart your server after each change.
If you want to get more detailed and/or explore more powerful plugin options, I recommend you start at the official Ruby on Rails Wiki.
3 Responses to “Create a Simple Ruby on Rails Plugin in Minutes”
I can’t get this to work..
I got the following instead
app/controllers/rss_feed_controller.rb:27: syntax error, unexpected kEND, expecting $end
RAILS_ROOT: ./script/../config/..
Any idea why?
I put this on my controller :
class RssFeedController #{title}”
count += 1
}
return titles_w_links
end
and the following on my viewer :
Exactly as the examples above.
By gerry leo nugroho on Sep 4, 2007
I would like to create a plugin which has one model and migration file & also the plugin is like as controller plugin type how can i implement this give me some guide line
By maheshbalaji on Aug 18, 2009