我试图将列表添加到具有多个选择的复选框。我在模型中有这个数组。
QOL = ["your health",
"your stress levels",
"how safe you feel",
"your education",
"your home"
]
我试图用simple_form形式做这样的事情。
<%= f.input :qolselections, as: :check_boxes, :collection => Goal::QOL %>
如何将选择保存到数据库并能够将其作为选定的字符串值进行检索?
答案 0 :(得分:0)
而不是常量,你应该考虑枚举:
class YourModel < ActiveRecord::Base
enum qol_selection: ["your health", "your stress levels", "how safe you feel", "your education", "your home"]
end
然后,您的解决方案变得类似于以下问题中描述的解决方案从Rails 4.1中选择保存枚举(https://stackoverflow.com/a/23686698/637094):
f.input :qol_selection, :as => :select, :collection => YourModel.qol_selections.keys.to_a