2012-02-06 15 views
7

私は現在、このような団体作っています:アクティブ管理にhas_many

プロジェクト

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

ユーザー

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 

を、私は、次の関連付けを持っています

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

役割

belongs_to :user 
belongs_to :project 
belongs_to :role_name 

ロール名

has_many :roles 

私は、私のフォームは何も起こりませんを介してこれを解決するための任意のアイデアを、ユーザの関連付けを破壊しようとすると? または、ショーブロックにリンクを削除するには?

答えて

16

は(attr_accessibleするとroles_attributes)あなたのプロジェクトのモデルにaccepts_nested_attributes_forを追加しよう:

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

ありがとうたくさん:) – Awea

+0

ありがとう、ありがとう、ありがとう –

4

allow_destroy:真は、この問題の根です。

関連する問題