3

I'm trying to make so that the user sees a number of options (I'm currently trying with check boxes) as selects multiple items, then turns into a single string joined by commas when the user submits it, ex:

<%= f.check_box :answer %><%= option.description %>
<%= f.check_box :answer %><%= option.description %>
<%= f.check_box :answer %><%= option.description %>

where :answer is an attribute in my Answer model and description is an attribute in my Option model, if multiple options are selected I want them to be saved as a single string like so:

"description1,description2,description3"

in order to save it to a single column in my database, how can I achieve this?

1 Answer 1

1

You can create multiple checkboxes adding them the multiple option, this way you can have:

<%= f.check_box :answer, { multiple: true }, 'first', false %>First
<%= f.check_box :answer, { multiple: true }, 'second', false %>Second
<%= f.check_box :answer, { multiple: true }, 'third', false %>Third

Note you'll also need to specify the false option to "can get rid" of all the unwanted values a group of multiple check_boxes generates, something like ["0", "0", "second", "0", "third"], that because of the unchecked default values, so, with the last parameter in your check_box helper, you get just ["second", "third"].

So, now having just an array is easier to join those values before persiting the object into the database, this can be done directly in the controller if you want:

@answer = Answer.new(answer_params)
@answer.answer = params['answer']['answer'].join(',')

Here you get the values from the params['answer'], the form, and specifically the checkboxes you've created, as it's an array you can use join to join them with a comma ,, so you'll get "second,third", and this way you can set the answer attribute for your Answer model with multiple checkboxes.

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

5 Comments

Thanks a lot! is there a way to call all the params in the second line? I have a variable number of options on each answer and for some reason it is not letting me save the answer to the data base can that be problem?
Do you mean to iterate over an options ActiveRecord result and get every option.description?
I'm currently creating a checkbox for every single option the question has, after checking the console I've traced the problem to being "Unpermitted parameter: answer", also can I add an if in the controller to avoid joining the params if it is already an string? i.e: An answer that the user responds with a text_field
After changing a couple of lines in my controller it works perfectly thank you very much! :D For future reference this the checker: if !params.kind_of?(String) @answer.answer = params['answer']['answer'].join(',')end
That's great, I still was thinking on how to do it (:.

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.