0

중첩 된 양식의 레벨을 고치 및 테이블로 성공적으로 구현했습니다. 그러나 다른 중첩 수준을 수행하는 방법에 대한 내 마음을 감싸는 데 어려움을 겪고 있습니다. 내 문제는 테이블을 사용하여이를 수행하는 방법입니다. 그리고 아마도 테이블은 전혀 쓸 방법이 아닙니다. 초보자를 도와 주셔서 감사합니다.난간 및 중첩 된 다중 레벨 중첩 된 양식

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 

그리고 :

여기
class Procedure < ApplicationRecord 
    belongs_to :procedure_category 
end 

내 최고 수준의 양식 코드 :

<%= form_for(@profession) do |f| %> 
    <%= render 'shared/profession_error_messages' %> 

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

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

    <%= f.label :active, class: "checkbox inline" do %> 
    <%= f.check_box :active %> 
    <span>Active profession?</span> 
    <% end %> 

    <table class='table'> 
    <thead> 
     <tr> 
     <th>Category</th> 
     <th>Description</th> 
     <th>Display Order</th> 
     <th>Selection Type</th> 
     <th>Delete</th> 
     <th>Edit</th> 
     </tr> 
    </thead> 
    <tbody class="categories"> 
     <%= f.fields_for :procedure_categories do |procedure_category| %> 
     <%= render 'procedure_category_fields', f: procedure_category %> 
     <% end %> 
    </tbody> 
    </table> 


    <%= link_to_add_association 'Add Category', f, :procedure_categories, 
    data: { association_insertion_node: '.categories', association_insertion_method: :append } %> 

    <br><br>  
    <%= f.submit "Save", class: "btn btn-primary" %> 
    <% end %> 
여기

내 모델입니다

그리고 다음 부분 한 수준 아래 :

<tr class="nested-fields"> 
    <td><%= f.text_field :category, class: 'form-control' %></td> 
    <td><%= f.text_field :description, class: 'form-control' %></td> 
    <td><%= f.text_field :display_order, class: 'form-control' %></td> 
    <% cs = options_for_select(controls, f.object.selection_type) %> 
    <td><%= f.select :selection_type, cs, class: 'form-control' %></td> 
    <td><%= link_to_remove_association "Remove Category", f %></td> 
    <% if f.object != nil %> 
    <td><%= link_to "Category", edit_procedure_category_path(@profession,f.object) %><td></td> 
    <% end %> 
</tr> 

그래서, 나는 (절차)를 중첩의 최종 레벨을 구현하는 방법과 사투를 벌인거야.

청취 해 주셔서 감사합니다.

class Profession < ApplicationRecord 
    has_many :procedures, through: categories 
    has_many :categories, dependent: :destroy 
    accepts_nested_attributes_for :categories, allow_destroy: true 
end 

category

class Category < ApplicationRecord 
    belongs_to :profession 
    has_many :procedures 

    accepts_nested_attributes_for :procedures, allow_destroy: true 
end 

procedure_category 모델의 이름을 바꿉니다 : 그리고 여기에 사용 has_many :through

+0

에서 읽기 : https://github.com/nathanvda/cocoon_simple_form_demo 상세한 답변 – nathanvda

답변

0

내 모델입니다

class Procedure < ApplicationRecord 
    belongs_to :category 
end 

내가 뭔가를 놓친 경우 확인할 수는 instruction from the rails guide

그들이보기에 사용할 수 있도록 professions#new 작업은 다음 변수를 만들어야합니다 컨트롤러 :

def new 
    @profession = Profession.new 
    @categories = @profession.categories.build 
    @procedures = @categories.procedures.build 
end 

뷰는 그 변수 때문에 사용 사용자 입력을 저장하고 parameters 해시에 저장된 입력을 사용하여 /profession/post 요청을 만듭니다.

<%= form_for(@profession) do |f| %> 
     <%= f.fields_for :categories do |category| %> 
       <%= category.fields_for :procedures do |precedure| %> 
       <% end %> 
     <% end %> 
<% end %> 

fields_for가 양식 작성기를 생성합니다. 매개 변수의 이름은 accepts_nested_attributes_for가 예상하는 이름이됩니다.그래서 양식에 post URL을 가리키고 있는지 확인

{ 
    'profession' => { 
    'name' => 'John Doe', 
    'categories_attributes' => { 
     '0' => { 
     'kind' => 'Home', 
     'street' => '221b Baker Street', 
     'procedures_attributes' => { 
     '0' => {}, 
     '1' => {} 
     } 
     }, 
     '1' => { 
     'kind' => 'Office', 
     'street' => '31 Spooner Street' 
     } 
    } 
    } 
} 

: 이것은 당신의 parameters 모습 방법입니다

: 2 개 주소를 가진 사용자를 생성 할 때처럼 예를 들어, 제출 된 매개 변수가 보일 것이다 /professions/ 및 라우팅은 binding pry을 설정 문제에 대한 professions#create 행동

def create 
    binding.pry 
end 

을 트리거하고 체크 것 귀하의 parameters이 어떻게 표시되는지 알 수 있습니다.

아마 데 도움이, 당신이 다른 중첩 된 관계와 수준이 입증 된 데모 프로젝트를 확인 했나보다 http://guides.rubyonrails.org/form_helpers.html#building-complex-forms

+0

감사합니다, 그러나 나는 당신이 다음을 통해 언급 한 이유를 이해하지 못합니다. 나는 many-to-many 관계를 구현하려고하고 있지 않다. 어떻게 도움이 되는가? – Foosaurus

+0

아니요 @ user8711275 맞습니다. 당신은 이해하지 못했습니다 (미안). 'has_many : through'는 다분히 많은 관계가 아니라, 일대 다 + 중첩 된 일대 다 관계입니다. 정확히 달성하고자하는 것이지만 더 좋은 방법입니다. 내 디자인이 더 뛰어나다. 내 코드가 더 좋다. 답변을 다시 읽은 다음 https://www.youtube.com/watch?v=Cj1FhPy9sz4를보고 http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association을 읽고 당신이 할 수있는 한 많은 정보 .. 곧 당신은 그것을 얻을 것이다! 행운을 빌어 요. –