How to omit attribute name when creating a checkbox

74 Views Asked by At

I have a "many to many" relationship between group and user, and I need to create a checkbox tag to put users in a group. So I tried to use fields_for for that. Like that:

# ...
- users.each do |user|
  f.fields_for "group[user_ids][]", user.group_users.build do |group_user_f|
    = group_user_f.check_box :user_id, {}, user.id, false

And the Rails produces:

<input checked="checked" id="group_user_ids__user_id" name="group[user_ids][][user_id]" type="checkbox" value="1">

But I need:

<input checked="checked" id="group_user_ids" name="group[user_ids][]" type="checkbox" value="1">

Without [user_id].

I can manually with check_box_tag, but I prefer like above, using fields_for.

1

There are 1 best solutions below

0
On BEST ANSWER

I solved with just:

= f.check_box :user_ids, **{multiple: true}**, user.id, false

I didn't know that the plural of an attribute it's also an attribute for use in an association.