Johshi Johshi - 2 months ago 4
Ruby Question

Strong params on a hash or array

In rails I did this in a model:

def update_user_ex(*attrs)
user.assign_attributes(*attrs)
# .............


where
attrs
is an array with of a hash(es), for simplicity let's say it's a single hash.

How can I do this in Rails 4? "permit" doesn't work on a hash or array.

Answer

It's possible to use strong parameters outside of controllers per the documentation at https://github.com/rails/strong_parameters

Example:

def update_user_ex(*attrs)
  params = ActionController::Parameters.new(attrs)
  user.assign_attributes(params.permit(:name, :email, :something_else))