I have a form, that among other things, contains about 20 different checkboxes. Like so:
<%= form_for @inventory do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
...
<p>
<%= f.check_box :apple %><%= f.label :apple %><br />
<%= f.check_box :banana %><%= f.label :banana %><br />
<%= f.check_box :orange %><%= f.label :orange %>
...
</p>
...
<% end %>
What I want to do is take the value of the selected checkbox, comma delimit them, and save them in a column in the db. So if the apple and orange checkbox is checked it saves as:
@inventory.fruit = "apple, orange"
how do I do this?
I don't think we can send multiple values as a string rather than an array. Look at the below solution
In Rails, how to handle multiple checked checkboxes, just split on the , or?
The solution is in pure HTML
code but you can use check_box_tag
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With