2012-02-06 28 views
7

Tôi hiện đang làm cho hiệp hội như thế này:tích cực quản trị has_many thông qua hiệp hội xóa

show do 
    h3 project.title 
    panel "Utilisateurs" do 
    table_for project.roles do 
     column "Prenom" do |role| 
     role.user.firstname 
     end 
     column "Nom" do |role| 
     role.user.lastname 
     end 
     column "email" do |role| 
     role.user.email 
     end 
     column "Role" do |role| 
     role.role_name.name 
     end 
    end 
    end 
end 

# override default form 
form do |f| 
    f.inputs "Details" do # Project's fields 
    f.input :title 
    f.input :code 
    end 

    f.has_many :roles do |app_f| 
    app_f.inputs do 
     # if object has id we can destroy it 
     if app_f.object.id 
     app_f.input :_destroy, :as => :boolean, :label => "Supprimer l'utilisateur du projet" 
     end 
     app_f.input :user,  :include_blank => false, :label_method => :to_label 
     app_f.input :role_name, :include_blank => false 
    end 
    end 
    f.buttons 
end 

Tôi có công ty sau đây:

Dự án

has_many :roles, :dependent => :destroy 
has_many :users, :through => :role 

tài

has_many :roles, :dependent => :destroy 
has_many :projects, :through => :role 

Vai trò

belongs_to :user 
belongs_to :project 
belongs_to :role_name 

RoleName

has_many :roles 

Khi tôi cố gắng phá hủy liên kết người dùng thông qua hình thức của tôi không có gì xảy ra, bất kỳ ý tưởng để giải quyết này? Hoặc để thêm liên kết xóa vào khối hiển thị của tôi?

Trả lời

16

Cố gắng thêm accepts_nested_attributes_for để mô hình dự án của bạn (và roles_attributes để attr_accessible):

class Project < ActiveRecord::Base 
    has_many :roles, :dependent => :destroy 
    has_many :users, :through => :role 
    accepts_nested_attributes_for :roles, :allow_destroy => true 

    attr_accessible :roles_attributes, (+ all you had here before) 
    ... 
end 
+0

Cảm ơn rất nhiều :) – Awea

+0

Cảm ơn bạn, cảm ơn bạn, cảm ơn bạn :) –

4

allow_destroy: true là gốc rễ của vấn đề này.

Các vấn đề liên quan