So I'm using simple_form for building my forms, this is not a requirement though.
What I am trying to do is using simple_forms collection_check_boxes and pass it an array.
I am storing 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'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
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.
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.tags
so that these tags is checked on rendering.
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?
Sidenote: This functionality should work with backbone too, in some places backbone would be used for adding tags.
How to use collection_check_boxes
with an Array
:
FRUITS = [[1, 'Abiu'], [2, 'Açaí'], [3, 'Assai'], [4, 'Acreola']]
<%= f.collection_check_boxes :fruits, FRUITS, :first, :last %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With