I followed the directions in Railscast #17 HABTM Checkboxes (revised) to get this code for adding services to a project using a has_and_belongs_to_many association:
<% Service.all.each do |service| %>
<%= hidden_field_tag "project[service_ids][]", nil %>
<%= check_box_tag "project[service_ids][]", service.id, @project.service_ids.include?(service.id), id: dom_id(service) %>
<%= label_tag dom_id(service), service.name %><br />
<% end %>
This works correctly, but I'd like to use Formtastic to generate the code in order to keep the formatting consistent with the rest of the page. The video mentions that Formtastic can do this easily, but I can't figure out the code for the life of me.
My guess was to do something like this:
<%= semantic_form_for :services do |f| %>
<%= f.input :name, :as => :check_boxes, :collection => Service.find(:all) %>
<% end %>
and that generates the list of services, but checking the boxes does not do anything. I know that last bit of code need to be linked somehow to the projects_services association, but I don't know how to do it.
Okay, I was trying to make this harder than it is. This is all I had to do:
<%= f.input :services, :as => :check_boxes %>