For a recent Rails project I had the following setup:

class Pager < ActiveRecord::Base
  has_and_belongs_to_many :users
  # rest ommited for brevity
end

In my view, I want to select 0-n users. Actually the first challenge was to create the checkboxes in a way the selected options get submitted as an array. It’s been ages since I last used checkboxes!

<input type="checkbox" name="pager[user_ids][]" value="1">
<input type="checkbox" name="pager[user_ids][]" value="2">
<input type="checkbox" name="pager[user_ids][]" value="3">

This way, the selected user IDs will get submitted as an array.

The next problem was Strong Parameters. Even though I added permit(:user_ids), I got an Error message saying something aling “Parameter ‘user_ids’ not allowed.”.

Digging in the API from ActionController::Parameters I discovered that only some scalar types are allowed. To allow Arrays, they have to be declared as such:

def pager_params
  params.reqire(:pager).permit(user_ids: [])
end

If you’re new to Strong Parameters, I highly recommend reading Strong Parameters by Example by sensible.io.

Spread the word!

share on Hacker News