Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在数组中使用集合复选框?_Ruby On Rails_Ruby On Rails 3_Tags_Simple Form - Fatal编程技术网

Ruby on rails 如何在数组中使用集合复选框?

Ruby on rails 如何在数组中使用集合复选框?,ruby-on-rails,ruby-on-rails-3,tags,simple-form,Ruby On Rails,Ruby On Rails 3,Tags,Simple Form,所以我使用simple_表单来构建表单,但这不是一个要求 我试图做的是使用简单的表单集合复选框并向其传递一个数组 我正在configatron中存储我的标签: configatron.tags = [{:name => "wheels", :tagtype => "property"}, {:name => "roof", :tagtype => "property"}, {:name => "doors", :tagtype => "property"}]

所以我使用simple_表单来构建表单,但这不是一个要求

我试图做的是使用简单的表单集合复选框并向其传递一个数组

我正在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

我只是想让集合复选框工作,然后在保存、构建标记并将其嵌入当前资源之前继续

我在某个地方读到,您可以映射到传入的集合中,并选择该集合中某个项目的内容。如果我做对了,重写value\u方法? 我好像不记得你是怎么做到的。我还想传入此资源的当前标记
:collection=>resource.tags
,以便在渲染时检查这些标记

有没有办法做到这一点?我将如何操作表单生成器使之成为可能,如果是,如何操作?我应该采取另一种方法吗

旁注:此功能也应适用于主干网,在某些地方主干网将用于添加标记。

检查后,我认为您需要将value\u方法和label\u方法作为符号传递给
collection\u复选框

例如:

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


这行吗?

如何使用
集合\u复选框
数组

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

<%= f.collection_check_boxes :fruits, FRUITS, :first, :last %>
FRUITS=[[1,'Abiu'],[2,'AçAí'],[3,'Assai'],[4,'acretola']]