Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Checkboxes, Convert to String, Single DB Column Rails

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?

like image 967
goddamnyouryan Avatar asked Aug 01 '11 17:08

goddamnyouryan


1 Answers

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.

like image 75
ashisrai_ Avatar answered Oct 27 '22 00:10

ashisrai_