How to remove empty parameters from params hash?

Rails 6.1+

Rails 6.1 added compact and compact! to ActionController::Parameters in order to remove nil values from params hash.

params.compact

params.compact!

Here is a link to PR.


Rails 6.1 added compact_blank and compact_blank! to ActionController::Parameters in order to remove blank values from params hash.

params.compact_blank

params.compact_blank!

Here is a link to PR.

Examples of blank values:

nil, '', ' ', [], {}, false

Put this after the hash:

.reject{|_, v| v.blank?}