所以我使用 simple_form 来构建我的表单,但这不是必需的。
我想要做的是使用 simple_forms collection_check_boxes 并将其传递给一个数组。
我将我的标签存储在 configatron 中:
configatron.tags = [{:name => "wheels", :tagtype => "property"}, {:name => "roof", :tagtype => "property"}, {:name => "doors", :tagtype => "property"}]
这是我的标签模型:
class Tag
include Mongoid::Document
embedded_in :taggable, polymorphic: true
field :name
field :tagtype
end
这是我尝试过的:
<%= f.collection_check_boxes :tags, @tags, @tags.map{|tag| tag.name}, @tags.map{|tag| tag.name} %>
哪里
@tags
设置为 configatron.tags
在 Controller 中我只是想让 collection_check_boxes 工作,然后在 before_save 上构建标签并将其嵌入到当前资源中。
我在某处读到过,您可以映射到传入的集合中并选择该集合中项目的内容。如果我做对了,覆盖 value_method 吗?
似乎不记得你是如何做到这一点的。我也想传入这个资源的当前标签
:collection => resource.tags
以便在渲染时检查这些标签。有没有办法做到这一点?我将如何操作 form_builder 以使其成为可能,如果是这样,如何操作?我应该采取另一种方法吗?
旁注:此功能也应与主干一起使用,在某些地方主干将用于添加标签。
最佳答案
检查后simple-form docs ,我认为您需要将 value_method 和 label_method 作为符号传递给 collection_check_boxes
比如这个:
<%= f.collection_check_boxes :tags, @tags, :name, :name %>
那样有用吗?
关于ruby-on-rails - 如何将 collection_check_boxes 与数组一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8517337/