Multiple checkboxes, conversion to row, individual columns of DB columns

I have a form that, among other things, contains about 20 different flags. For instance:

<%= 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, separate them with a comma and save in a column in db. Therefore, if the apple and orange check box is checked, it is saved as:

@ inventory.fruit = "apple, orange"

how to do it?

+5
source share
1 answer

I don't think we can send multiple values ​​as a string, not an array. See the solution below

In Rails, how to handle multiple checked flags, just split them or?

HTML, check_box_tag.

+5

All Articles