Rails simple_form check_boxes将多个选择保存到数据库

时间:2016-01-21 19:16:43

标签: ruby-on-rails ruby-on-rails-4 simple-form

我试图将列表添加到具有多个选择的复选框。我在模型中有这个数组。

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 %>

如何将选择保存到数据库并能够将其作为选定的字符串值进行检索?

1 个答案:

答案 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