6

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.