如何将collection_check_boxes与数组一起使用?

时间:2022-12-06 07:31:14

So I'm using simple_form for building my forms, this is not a requirement though.

所以我使用simple_form来构建表单,但这不是必需的。

What I am trying to do is using simple_forms collection_check_boxes and pass it an array.

我想要做的是使用simple_forms collection_check_boxes并传递一个数组。

I am storing my tags in configatron:

我将我的标签存储在configatron中:

configatron.tags = [{:name => "wheels", :tagtype => "property"}, {:name => "roof", :tagtype => "property"}, {:name => "doors", :tagtype => "property"}]

Here is my Tag model:

这是我的Tag模型:

class Tag
  include Mongoid::Document
  embedded_in :taggable, polymorphic: true

  field :name
  field :tagtype
end

Here is what I've tried:

这是我尝试过的:

<%= f.collection_check_boxes :tags, @tags, @tags.map{|tag| tag.name}, @tags.map{|tag| tag.name} %>

where @tags is set to configatron.tags in controller

其中@tags在控制器中设置为configatron.tags

I simply want to make the collection_check_boxes work and then on before_save build the tag and embed it in the current resource.

我只想让collection_check_boxes工作,然后在before_save上构建标记并将其嵌入当前资源中。

I've read somewhere that you can map into the collection passed in and pick the content of an item of that collection. If i get it right, override the value_method? Can't seem to remember how you can do this though. I also want to pass in the current tags of this resource :collection => resource.tagsso that these tags is checked on rendering.

我已经阅读过某个地方,你可以映射到传入的集合中并选择该集合的项目内容。如果我做对了,请覆盖value_method?似乎无法记住你怎么能这样做。我还想传递此资源的当前标记:collection => resource.tagsso,在渲染时检查这些标记。

Is there any way of doing this? How would I manipulate the form_builder to make this possible, if so, how? Should I take another approach?

有没有办法做到这一点?我如何操纵form_builder使这成为可能,如果是这样,怎么样?我应该采取另一种方法吗?

Sidenote: This functionality should work with backbone too, in some places backbone would be used for adding tags.

旁注:此功能也应与骨干网一起使用,在某些地方,骨干网将用于添加标签。

2 个解决方案

#1


3  

After checking simple-form docs, I think you need to pass in the value_method and label_method as symbols to collection_check_boxes

检查简单形式的文档后,我认为您需要将value_method和label_method作为符号传递给collection_check_boxes

Such as this:

如:

<%= f.collection_check_boxes :tags, @tags, :name, :name %>

Does that work?

那样有用吗?

#2


6  

How to use collection_check_boxes with an Array:

如何将collection_check_boxes与数组一起使用:

FRUITS = [[1, 'Abiu'], [2, 'Açaí'], [3, 'Assai'], [4, 'Acreola']]

<%= f.collection_check_boxes :fruits, FRUITS, :first, :last %>

#1


3  

After checking simple-form docs, I think you need to pass in the value_method and label_method as symbols to collection_check_boxes

检查简单形式的文档后,我认为您需要将value_method和label_method作为符号传递给collection_check_boxes

Such as this:

如:

<%= f.collection_check_boxes :tags, @tags, :name, :name %>

Does that work?

那样有用吗?

#2


6  

How to use collection_check_boxes with an Array:

如何将collection_check_boxes与数组一起使用:

FRUITS = [[1, 'Abiu'], [2, 'Açaí'], [3, 'Assai'], [4, 'Acreola']]

<%= f.collection_check_boxes :fruits, FRUITS, :first, :last %>