Adds a DSL to ActionController that allows to add strong params filtering without adding a specific, dirty controller method.
DSL Changes in 0.0.3
- No more
create_paramsor such, filtered and whitelisted attributes are now available via the regular
Rails's Strong Parameters is obviously a better approach than having the model decide which attributes are protected or not. The only problem I have with this is the need to add an ugly method (at least one) to the controller to filter and whitelist the parameters.
BetterStrongParams is simply a way to create those methods via DSL instead of a manually typing them, seems like it is a more naturally looking interface for a good idea.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install better_strong_params
BetterStrongParams is automatically included in ActionController::Base, so you don't actually need to do anything to set this one up.
BetterStrongParams allows you to use the
whitelist_parameters method in your controllers:
class UsersController < ApplicationController whitelist_parameters post: [:title, :body], user: [:name, :age], .... end
whitelist_parameters a representation of a hash as you would enter in normal Strong Parameters method:
top_level_key: [attribute, attribute], another_top_level: [...], ...
Unlike many other Strong Parameters implementations in which you would have to define a sanitizing method (
user_params), BetterStrongParams lets you keep on using the regular
class UsersController < ApplicationController whitelist_parameters user: [:name, :age] def create @user = User.new(params[:user]) # => params[:user] is being whitelisted without the need to define / create the extra method. if @user.save redirect_to treasure_url else redirect_to jail_url end end end
- Fork it ( http://github.com/
- 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