Useful Rails Plugins

来源:互联网 发布:视频直播聊天室源码 编辑:程序博客网 时间:2024/05/21 10:17

Useful Rails Plugins

In the process of developing our new site using Ruby on Rails I have come across quite a few plugins that I thought I would share. Of course, there are an endless number of plugins available for Rails, with many more becoming available every day. In case you are curious, a “plugin” is a set of files that adds functionality to your Rails application. Installation of plugins is very simple, as with most things in Rails. You simple type script/plugin install plugin_name from a command line in your application, and the plugin gets installed for you. So without going into too much detail about what plugins are, let’s take a look at some of the plugins that I have found helpful recently:

will_paginate plugin

Pagination was removed from the Rails 2.0 core code, with the developers opting to move pagination functions to plugins instead. The result is that there are many pagination options to choose from, including code it up yourself if you feel like it. I don’t, and prefer to use this plugin. Simply install the plugin, check out the README file included with it, and you are up and running with paginated pages in no time. A nice, very easy way to display multiple pages of data, and not go too heavy on the database while you are doing it.

I did find that by default the pagination will add a “page” variable to the URL string which it needs to determine which page of data. Of course, this is a good thing, but I prefer not to have ?page=4 at the end of my URLs. The solution is to simple create a route in your config/routes.rb file that recognizes this variable, giving you URLs that end in /4 rather than the variable/value pair from before. A bit more search engine friendly and easier on the eyes.

script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

Active Merchant plugin

If you are integrating any type of ecommerce or payment system into your Rails application, I’m not sure that there is a more helpful plugin than this one. The Active Merchant plugin was developed by the people that created Shopify.com, and is available as a plugin. As far as plugins go, this one is rather large, but comes with pretty good documentation. Essentially, Active Merchant gives you the ability to submit payments to a ton of different gateways. There is not much more I can say about this plugin other than it is a life saver.

script/plugin install active_merchant

RESTful Authentication plugin

Rather than re-inventing the wheel with regard to authenticating and authorizing users, I prefer to user this plugin. In addition to providing a nice system for creating user accounts, this plugin takes care of authenticating users (figuring out if they are who they claim to be) and also makes it extremely easy to authorize users (determine if they are allowed to do what they are trying to do). A simple system for suspending users right out of the box, but a robust foundation to build a user management system on. I really like this plugin, particularly because it takes advantage of RESTful architecture. For me, combining this plugin with the acts_as_state_machine plugin that I have previously written about has been extremely helpful.

script/plugin install restful_authentication

ssl_requirement plugin

This is a small plugin that was written by DHH himself. It does exactly what the name says that it does, and allows a simple way to tell your controllers whether or not they should be secured. Simply install the plugin, then declare the actions in your controllers that need to be secure, and this plugin will ensure that it happens. If a user tries to access an action that should be secure without the “https://” in the URL, the plugin will automatically redirect them to the secure URL. Or you can enforce the opposite, and make pages redirect to non-secure as well.

The only thing to point out here is that the plugin works as a before_filter on your controllers, so be careful where you put the code in your controller, in the event that you have other filters that need to run prior to the re-direct happening. Small thing, but it can be confusing if you aren’t sure why an error is occurring.

script/plugin install ssl_requirement

validates_multiparameter_assignments plugin

This is another small plugin, but it is super useful to me. For some reason when you submit a form that creates or updates multiple models, there is an issue with the validation. More specifically, you don’t get the nice feedback that you should get when something fails, such as when you include in your forms. Don’t ask me, I’m not sure why it happens, or what the technical explanation is. What I do know is that they plugin allows you to run model validations on multiple models from one form. In other words, I can submit a form that creates a user record and a profile record from the data in the form, and Rails will have no problem running validations on each of the models, and also reporting back to me.

Of course, if you are targeting more than one model in your forms, as in my example of a user model and a profile model, you will want to be sure to include two blocks in your views. That way you are sure to get the feedback that you want if validation fails on either model.

Download ZIP File

acts_as_taggable_on_steroids plugin

This plugin is an improvement on the popular acts_as_taggable plugin. Essentially this plugin gives you the ability to use tagging on your site, with any model that you want. Simply install the plugin to your Rails application, generate the migration file (check the README), migrate your database, and then include the line acts_as_taggable in the model that you want to use tagging with.

The plugin provides everything you need to get going with tagging. There are even methods included to easily generate a tag cloud, search for tagged content, and more. Also, create a migration file to change the model you are tagging so that there is a varchar field called “cached_tag_list”. The plugin will automatically cache the tags for that record in the model itself, which takes a load off of the database when displaying tags. The cached tags are updated automatically, so all you have to do is create the field in the database.

script/plugin install http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids
From: http://www.practicalecommerce.com/blogs/developers-diary/archives/123
原创粉丝点击