#17 HABTM Checkboxes

It is often asked: how do I create a list of checkboxes for managing a HABTM association? Ask no more because this episode will show you how to do exactly that.
<!-- products/_form.rhtml -->
<% for category in Category.find(:all) %>
<div>
  <%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %>
  <%= category.name %>
</div>
<% end %>
# products_controller.rb
def update
  params[:product][:category_ids] ||= []
  #...
end

你可能感兴趣的:(checkbox)