I have a project model and a categories model that has no relation. In the form for the project model I would like to be able to select categories (check boxes) that the project belongs to (there can be many). Categories have one string heading and an array of categories that belongs to that heading.
I have this code without using simple_form it lists categories and check boxes, works like a charm. How would I do this using simple_form?
<% @categories.each do |category| %>
<b><%= category.heading %></b><br />
<% category.categories.each do |subcategory| %>
<%= check_box_tag "project[categories][]", subcategory,@project.categories.include(subcategory) %><%=subcategory%><br />
<% end %><br />
<% end %>
The model which form i try to update is as:
class Project
include Mongoid::Document
...
field :categories, type: Array
...
end
Categories are incidentally also stored in a Mongoid document but has no to projects relation.
class Category
include Mongoid::Document
field :heading, type: String
field :categories, type: Array # should probably be called subcategories...
end
I’m unsure what you want exactly, but you can try something like this. If you can clarify what you’re asking, I’ll try to give a more detailed answer.
where
listOfCategoryNamesis an array of strings of all the category names.