2017-11-14 4 views
0

내 문제는 최고의 사진과 함께 설명되어 있습니다link_to_add_association는

class Profession < ApplicationRecord 
    has_many :procedure_categories, dependent: :destroy 
    accepts_nested_attributes_for :procedure_categories, allow_destroy: true 
end 

둘째 :

class ProcedureCategory < ApplicationRecord 
    belongs_to :profession 
    has_many :procedures 

    accepts_nested_attributes_for :procedures, allow_destroy: true 
end 
다음 extra forms added issue

톱 모델

셋째 :

class Procedure < ApplicationRecord 
    belongs_to :procedure_category 
end 

여기 코드가 link_to_add_association 메소드를 포함하여 문제의 원인입니다.

<div class="nested-fields"> 
    <%= f.label :category %> 
    <%= f.text_field :category, class: 'form-control' %> 

    <%= f.label :description %> 
    <%= f.text_field :description, class: 'form-control' %> 

    <%= f.label :display_order %> 
    <%= f.text_field :display_order, class: 'form-control' %> 
    <% cs = options_for_select(controls, f.object.selection_type) %> 

    <%= f.label :selection_type %> 
    <%= f.select :selection_type, cs, class: 'form-control' %> 

    <table class='table'> 
    <thead> 
     <tr> 
     <th>Category</th> 
     <th>Description</th> 
     <th>Display Order</th> 
     <th>Selection Type</th> 
     </tr> 
    </thead> 
    <tbody class="procedures"> 
     <%= f.fields_for :procedures do |procedure| %> 
     <%= render 'procedure_fields', f: procedure %> 
     <% end %> 
    </tbody> 
    </table> 


    <%= link_to_add_association 'Add Skill', f, :procedures, 
    data: { association_insertion_node: '.procedures', association_insertion_method: :append } %> 
    <br><br> 

    <%= link_to_remove_association "Remove Category", f %> 
</div> 

마지막으로, 여기 절차를 렌더링 내 부분입니다 :

<tr class="nested-fields"> 

    <td><%= f.text_field :skill, class: 'form-control' %></td> 
    <td><%= f.text_field :description, class: 'form-control' %></td> 
    <td><%= f.text_field :display_order, class: 'form-control' %></td> 

    <td><%= link_to_remove_association "Remove Skill", f %></td> 
</tr> 

이 모양을 가진 주셔서 감사합니다.

+0

'.procedures' 클래스가 페이지에 두 번 존재합니까? (각'Category' 블록 아래에 ...) 그리고 나서'.procedures' 클래스에 추가하고 있습니다, 네? – jvillian

+0

아, 이제 알겠습니다. 그래서 이것을 제거하면 : association_insertion_node : '.procedures'는 문제를 해결하지만, 형식이 잘못되었습니다. association_insertion_node를 사용하여 서식을 유지할 수있는 방법에 대한 제안이 있습니까? – Foosaurus

+0

물론입니다. 고유 한 '# id'(예 :'# procedure-category-6'가 '6'이 동적으로 할당 된)에 각 'Category' 블록을 래핑 한 다음 삽입을 타겟팅하기 위해'# procedure-category-6. 아니면 그 라인을 따라 뭔가. 나는'link_to_add_association'을 사용하지 않으므로, 구문을 수정해야하는지 잘 모르겠습니다. – jvillian

답변

2

Category 섹션 아래에 .procedures이 있다고 생각됩니다. 이 경우 .procedures 클래스의 두 인스턴스에 모두 추가됩니다.

Category 섹션에 고유 한 식별자를 만들어야합니다 (예 : #procedure-category-6, 여기서 6은 동적으로 할당 됨). #procedure-category-6 .procedures을 삽입 타겟팅에 사용하십시오.