google was not very friendly about this.
I have a Service Model which saves also a relation to a Category Model.
class Service
include Mongoid::Document
has_and_belongs_to_many :categories, inverse_of: :service
end
class Category
include Mongoid::Document
has_and_belongs_to_many :services, inverse_of: :category
end
services/_form.html.erb
<%= f.label "Category" %>
<% @categories.each do |category| %>
<%= check_box_tag "service[category_ids][]", category.id, @service.category_ids.include?(category.id), id: dom_id(category) %>
<%= label_tag dom_id(category), category.name %><br>
<% end %>
Can someone explain how to delete existing unchecked check_boxes?
class Pro::ServicesController < ApplicationController
def update
@service = Service.find(params[:id])
if params[:service][:category_ids]
params[:service] ||= {}
params[:service][:category_ids] ||= []
# no clue how to delete existing relations
# params[:service][:category_ids] = ["1234567890","098765432"]
end
respond_to do |format|
if @service.update_attributes!(params[:service])
format.html { redirect_to [:pro, @service], notice: 'Service was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @service.errors, status: :unprocessable_entity }
end
end
end
end
many thanks!
simply put this line into you update_attributes: