Generate list of columns for strong parameters in rails

The strong_params gem is now integrated into the core of rails as of version 4.0. With this new functionality Action Controller parameters are forbidden to be used in Active Model mass assignments until they have been whitelisted. To use strong_params, you simply call a protected method to pass attributes to ActiveRecord when creating or editing models in your controller. Something like:

  def user_params
    params.require(:user).permit(:first_name, :last_name, :email, :phone )

This list of attributes can get kind of long and in order to create the list I am constantly going back and forth between schema.rb and my controller file. There is an easier way to generate this list. Just run the following in your rails console replacing Model with your actual table name: {|c| }

I know it isn’t a huge deal to create the list by hand, but I love the simplicity of just running this in the console and getting my list of attributes.

View other articles in: Code