How to use collection_check_boxes with an array?

Therefore, I use simple_form to create my forms, but this is not a requirement.

What I'm trying to do is use simple_forms collection_check_boxes and pass an array to it.

I save my tags in configatron:

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

Here is my tag model:

 class Tag include Mongoid::Document embedded_in :taggable, polymorphic: true field :name field :tagtype end 

Here is what I tried:

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

where @tags set to configatron.tags in the controller

I just want work_check_boxes to work, and then before_save created the tag and inserted it into the current resource.

I read somewhere that you can map to a collected collection and select the contents of an element of this collection. If I get it right, override value_method? I don’t seem to remember how you can do this. I also want to pass the current tags of this resource :collection => resource.tags so that these tags are marked when rendering.

Is there any way to do this? How can I manipulate form_builder to make this possible, if so, how? Should I use a different approach?

Sidenote: this functionality should also work with the main trunk, in some places the base chain will be used to add tags.

+7
source share
2 answers

After checking the simple forms , I think you need to pass the values ​​of value_method and label_method as characters to collection_check_boxes

For example:

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

It works?

+3
source

How to use collection_check_boxes with Array :

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

All Articles