0

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"}
Vucko
  • 20,555
  • 10
  • 56
  • 107
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • `...permit(customer_ids: [])` is the right syntax. Something else is going on in your code. I suggest you look elsewhere. The other thing I can think of is that, for some reason, the column isn't being recognised as an array. – Mohamad Mar 24 '16 at 10:21
  • try like this `def user_params params.require(:user).permit( :customer_ids => [] ) # pg array column end` – Narasimha Reddy - Geeker Mar 24 '16 at 10:22
  • @Mohamad where? I mean I debugged it and all looks good, but it either says unpermitted params, or, when I change to `permit(:customer_ids)` it just does not update the `customer_ids` – Andrey Deineko Mar 24 '16 at 10:23
  • 1
    I think in that case `.permit(:customer_ids)` should work – Rajdeep Singh Mar 24 '16 at 10:23
  • add `{}` in the form field `options_from_collection_for_select(customers, 'id', 'name', user.customer_ids), {},{ include_blank: 'Select customer', multiple: true, size: 15 }, class: 'form-control'` . it should work. try this – Narasimha Reddy - Geeker Mar 24 '16 at 11:38

2 Answers2

2

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.

Mohamad
  • 34,731
  • 32
  • 140
  • 219
0

Try changing your select tag like this

= f.select(:customer_ids, customers.collect { |c| [ c.name, c.id ] }, 
                          { prompt: "Select Customer"}, 
                          { multiple: true, size: 5, class: 'form-control' })
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78