0

我正在使用simple_form并有以下示例标签:

<%= f.input :medical_conditions, :label=>false, :collection => medical_conditons, :as => :check_boxes%>

该集合包含大约 100 个复选框。但是,当用户只选择 1 或 2 时,所有内容仍会保存到数据库中,如下所示:

---
- ""
- ""
- ""

medical_conditions是我的一个简单数组application_helper

def medical_conditons
t = [
    "Allergies/Hay Fever",
    "Diabetes",
    "Heart Surgery"]
return t
end

medical_conditions领域是一个:string领域。

我需要做什么才能仅以逗号分隔的方式保存选择的值。

4

2 回答 2

1

在你的控制器中尝试这样的事情(猜测你是如何编写创建/更新方法的)......

params[:medical_conditions].delete('') #this will remove the empty strings
@instance.update_attribute(:medical_conditions, params[:medical_conditions].join(','))
#or however you want to save the input, but the key is the .join(',') which will
#create a comma-separated string of only the selected values, which is exactly
#what you're looking for me thinks :-)

如果这对您有用,我会考虑制作一个私有帮助方法来为您格式化参数,以便您可以在#create、#update 或任何其他需要它的地方使用它。这应该让你的 crud 动作更干净,更“rails way-ish”。

于 2011-07-13T23:49:16.340 回答
1

这不是 simple_form 行为。它来自 Rails。看到这个:http ://d.pr/6O2S

于 2011-05-07T01:01:27.500 回答