2013-12-16 2 views
0

구글은 이것에 대해별로 친절하지 않았습니다.체크하지 않은 값을 삭제하지 않는 체크 박스가있는 몽고이드 update_attributes

저는 카테고리 모델과의 관계를 저장하는 서비스 모델을 가지고 있습니다.

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 

서비스/_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 %> 

사람은 기존의 체크되지 않은 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 

많은 감사! 당신이 update_attributes에

답변

0

은 단순히이 줄을 넣어 :

@service.category_ids = params[:service][:category_ids]