ParamsSanitizer
params_sanitizer sanitize parameter.It is really easy and useful.
Installation
Add this line to your application's Gemfile:
gem 'params_sanitizer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install params_sanitizer
Usage
For example. sanitize params for a search query.
Define sanitizer.
class SearchParamsSanitizer < ParamsSanitizer::Base
def self.permit_filter
[:word, :order]
end
exist_value :word, '' # set default value '', when param[:word] does not exist.
accept_value :order, 1 , ['0','1'] # set default value 1, when param[:order] is not 0 or 1.
end
other sanitizer look this. ParamsSanitizer::Sanitizers
and in controller
def search_params
SearchParamsSanitizer.sanitize(params) # can get sanitized params.
end
result.
{
word: 'japanese anime',
unknown_params: 'hogehogehoge',
}
# after sanitize
{
word: 'japanese anime',
order: 1
}
when params nest.
{
search: { word: 'japanese anime' }
}
def search_params
SearchParamsSanitizer.sanitize(params, :search) # can get sanitized params.
end
result.
{
word: 'japanese anime',
order: 1
}
Sanitizers
API DOCUMENT
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request