Clean Params is a Ruby gem for use with Rails to access the controller parameters cleanly. No more ugly hash accesses. No more flooding your codebase with multiple condition checking. With clean_params you can specify various rules to parse your params hash inside the initializer file. It also allows for nested hash parsing.
Add this line to your application's Gemfile:
gem 'clean_params'
Run the generator to create the initializer file. You can specify your parsing rules here.
rails g clean_params:install
Example initializer file -
CleanParams.configure do |config|
config.params = {
# Customize your params here
'key' => [['key', 'Key'], 'default'],
'key2' => [['key2', 'Key2'], 'key2']
}
end
Example params hash -
params = {"utf8"=>"✓", "authenticity_token"=>"123", "Key" => "key", "token"=>"323344", "customer_detail"=>{"caller_id"=>"123", "customer_name"=>"XYZ"}}
And goodies -
params = CleanParams.clean(params) # params - rails params hash
params.key # => key
params.key2 # => key2
params.token # => 323344
params.caller_id # => 123
Or you can include it inside the filter in ApplicationController and use the object inside any controller.
- 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