1

I'm trying to have a list to checkboxes with multiple selection. I have this array in the model.

QOL = ["your health", 
    "your stress levels",
    "how safe you feel",        
    "your education",
    "your home"
]

I'm trying to do somthing like this in my simple_form form.

<%= f.input :qolselections, as: :check_boxes, :collection => Goal::QOL %>

How can I save the selections to the database and be able to retrieve them as the selected string values?

tet5uo
  • 139
  • 1
  • 1
  • 12

1 Answers1

0

Instead of a Constant, you should consider an enum:

class YourModel < ActiveRecord::Base
  enum qol_selection: ["your health", "your stress levels", "how safe you feel", "your education", "your home"]
end

Then, your solution becomes similart to the one described in the following question Saving enum from select in Rails 4.1 (https://stackoverflow.com/a/23686698/637094):

f.input :qol_selection, :as => :select, :collection => YourModel.qol_selections.keys.to_a
Community
  • 1
  • 1
Leonel Galán
  • 6,993
  • 2
  • 41
  • 60
  • Thanks @Leito. What I am really interested in is being able to store multiple selections. As in two values from the enum at the same time. Am I missing this from the post you linked? – tet5uo Jan 21 '16 at 21:22
  • Nope, I dropped the ball. That solution won't help. Sorry about that. Quick thought: You could serialize the response to convert and array into a single string? – Leonel Galán Jan 21 '16 at 21:26