M
M
Marina2014-12-23 14:44:41
Ruby on Rails
Marina, 2014-12-23 14:44:41

How to store multiple values ​​from multi checkbox in 1 field of string type database?

It is necessary to store several values ​​from the checkbox with multiple selection in a database field of type string

<input id="checkbox-1"  type="checkbox" value="drama" name="user[book][]">
<input id="checkbox-2"  type="checkbox" value="poem" name="user[book][]">
<input id="checkbox-3" type="checkbox" value="tragicomedy" name="user[book][]">

those. it is assumed that if, for example, 1 and 3 checkboxes are selected, then it will be written to the database like this: "drama, tragicomedy"
how can this be done when adding a new user?
The controller uses the following structure:
private
  def user_params
      params.require(:user).permit(.....)
  end

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vsuhachev, 2014-12-24
@vsuhachev

For example, you can use serialization

S
Sergey Krasnodemsky, 2014-12-23
@Prognosticator

Without irony, I say that you formulated the question so competently that you answered yourself. Cast user[book] to the string you want and save.
params.requier(:user).permit(:your_string_field)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question