The problem with plataformatec simple_form checkboxes and the fix

Plataformatec Simple Form is a popular gem, helping Rails developers to build forms. I have experienced a problem with checkboxes and managed to fix it.

The problem

When I use a checkbox in simple form, like:

1
2
3
4
<%= f.input :subject, as: :check_boxes,
collection: subject_list,
input_html: { multiple: true }
%>

The checkbox shows ok and I can see that the ticked checkbox items have been saved in Rails console. However, when I want to edit the saved data, non of the checkbox items have been ticked.

The fix

The fix is quite simple, e.g.:

1
2
3
4
5
<%= f.input :subject, as: :check_boxes,
collection: subject_list,
value_method: lambda {|v| v.last.to_s },
input_html: { multiple: true }
%>
Share