No commit activity in last 3 years
No release in over 3 years
Provides a light DSL for defining strong parameters in Rails controllers
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

StrongParametersDsl

The missing Rails controller class method that lets you declare your strong parameters.

Installation

Add this line to your application's Gemfile:

gem 'strong_parameters_dsl'

Or install it yourself as:

$ gem install strong_parameters_dsl

Usage

When Rails came out with Strong Parameters it just didn't feel Ruby enough. This gem is a simple workaround that will let you declaratively define your strong parameters as you would your filters.

class UsersController < ActionController::Base
  strong_params :user do
    permit :name, :email, comments_attributes: [:comment, :post_id]
  end
  
  # ... or ...
  strong_params :user, permit: [:name, :email, comments_attributes: [:comment, :post_id]]
  
  # an additional feature added is the ability to permit a key whose value is an arbitrary hash
  strong_params :user, any: :data

  # you can also mix and match any of the above approaches
  strong_params :user, any: :data, permit: :name do
    permit :more_keys, :and_stuff
  end


  def create
    # the named param method is created based on the argument passed
    # to the strong_params call
    User.create user_params

    # ... rest of code ...
  end
end

The block passed to strong_params is evaluated in the context of the ActionController::Parameters instance that is returned by params. Or more specifically by the strongified instance returned by calling params.require(:some_key).

There is an issue regarding allowing arbitrary hashes in some keys which was debated here. The solution wasn't very neat but it could be added as an instance method on the Parameters instance and then used as part of this DSL. Ideas welcome.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request