It is a Ruby on Rails plugin that provides param_protected and param_accessible methods on controllers analogous to the attr_protected and attr_accessible methods for models.
It is a very simple -- all it does it filter out specified parameters from a request.
Why?
Good question... you can ready about why attr_protected sucks here, or you can just read my following little rant...
What's the goal of attr_protected? To protect us from user input, not from ourselves. When I used attr_protected, I had to refactor tons of code in models, controllers and tests (that already worked well) to not use the mass attribute setters.
Was this massive code refactoring really worth the protection from the very few places were I do something like:
User.new(params[:user])
or
User.update_attributes(params[:user])
Truth of the matter, I was hardly ever passing params (or a subset thereof) to a mass attribute setter. So no, it wasn't worth the massive refactoring job.
Installation
git clone git://github.com/cjbottaro/param_protected.git vendor/plugins/param_protected
Usage
class UsersController < ApplicationController
param_protected :user_id
end
class AccountController < ApplicationController
param_accessible :account_id
end
param_protected is used to blacklist and param_accessible is used to whitelist.
You can give it an array of param names to filter:
param_protected [:user_id, :some_other_param]
param_protected and param_accessible are both just before filters, so the usual :only and :except arguments can be used:
param_protected :user_id, :only => :some_action
param_protected :user_id, :only => [:some_action, :another_action]
param_protected :user_id, :except => :some_action
param_protected :user_id, :except => [:some_action, :another_action]
You can protect nested params also (removes params[:user][:user_id]):
param_protected 'user/user_id'
param_protected is aware of array params and handles them properly.
Caveats (IMPORTANT!!!!)
Because param_protected is really a before filter (uses prepend_before_filter), you must take special care to ensure that it runs before any of your other before filters!! If it is not, some of your before filters might have access to some params they shouldn't.
Tests
rake test should work (from the plugin's root dir), though it's far from comprehensive.
Documentation
Please see the
README for usage instructions and examples.