Search code examples
ruby-on-railsruby-on-rails-4strong-parameters

Unpermitted params customer_ids


Super simple, dumb thing, which I can't figure out for more, than an hour now:

def user_params
  params.require(:user).permit(customer_ids: []) # pg array column
end

My form:

= f.select :customer_ids,
    options_from_collection_for_select(customers, 'id', 'name', user.customer_ids),
    { include_blank: 'Select customer', multiple: true, size: 15 },
    class: 'form-control'

And while updating user I'm getting

Unpermitted parameter: customer_ids

How the heck in the world is that possible?

Parameters: {"utf8"=>"✓", "authenticity_token"=>"oCkUEi2pNajM0ydHUH2w6iYIq5eKjfCY5ig9U2qDTXxMqECCgQ2Dn9YtqkMqXlTmLl5q/OO8x23o/P50SnmgUg==", "user"=>{"customer_ids"=>"84"}, "commit"=>"Assign selected customer to user", "id"=>"2"}

Solution

  • Your form isn't sending in the customer_ids parameters as an array.

    "user"=>{"customer_ids"=>"84"}
    

    This is why. It should be (notice the square brackets):

    "user"=>{"customer_ids"=>"[84]"}
    

    If you declare the param as an array, it should be posted as an array. This is likely an issue in your form.

    Usually, I would use checkboxes for something like this, but this depends on your user interface. Here's something similar I have done in the past.

    = f.collection_check_boxes :customers, customers, :id, :name do |cb|
      = cb.label
        span.pull-right = cb.check_box
    

    Look at the collection form helpers in Rails. A multiselect should work, but I have not used one this way.