2016-10-29 2 views
1

레일 5 및 간단한 양식을 사용하여 앱을 작성하고 있습니다. Twitter Bootstrap을 사용하여 각각의 중첩 된 모델의 필드를 다른 탭이나 열에 표시하려고합니다. 여기에 기존의 상위 레코드 (여기에서는 "문서 유형", 중첩 모델의 기존 레코드 (여기서는 "키워드"))가 새로 만들어 지거나 편집 될 때이 _ 예가 있습니다. _destroy를 사용한 삭제는 _destroy .레일 Simple_form (중첩) - 저장시 중복 항목

별도로 만들고 삭제할 수 있습니다. 이제 부모에게 추가하고 수정을 허용하려고합니다. 내가 더 많이 사용하게 될 것입니다. 그것을 해결

를이 내 모델입니다 것은?

class Documenttype < ApplicationRecord 
    has_many :annotations, dependent: :restrict_with_error 
    has_many :documents, dependent: :restrict_with_error 
    has_many :tagtypes, dependent: :restrict_with_error 
    has_many :keywords, dependent: :destroy 

    accepts_nested_attributes_for :keywords, allow_destroy: true 

    validates :name, presence: true, uniqueness: true, length: { minimum: 5 } 
    scope :active, -> { where(active: true) } 
end 

class Keyword < ApplicationRecord 
    belongs_to :documenttype 

    validates :keywords, presence: true 
    validates :language, presence: true 
end 

이는 형태 :

이 문제에서 배우는 0
<%= simple_form_for @documenttype, html: { class: 'form-horizontal', multipart: true }, 
    wrapper: :horizontal_form, 
    wrapper_mappings: { 
     check_boxes: :horizontal_radio_and_checkboxes, 
     radio_buttons: :horizontal_radio_and_checkboxes, 
     boolean: :horizontal_boolean 
    } do |f| %> 

    <div class="btn-toolbar btn-group" role="toolbar"> 
     <%= f.button :submit, :class => "btn btn-xs btn-default" %> <%= link_to 'List' , documenttypes_path, :class => "btn btn-xs btn-default" %> 
    </div> 

    <h4>Document Type</h4> 

    <div class="col-md-6"> 

    <%= f.error_notification %> 

    <%= f.input :name, placeholder: 'Enter name' %> 

    <%= f.input :description, placeholder: 'Description' %> 

    <%= f.input :active, as: :boolean %> 

    </div> 
    <div class="col-md-6"> 
    <%= f.simple_fields_for :keywords do |ff| %> 
     <div class="panel panel-body panel-default"> 
     <div class="col-md-3"> 
     <%= ff.input :language, :collection => ["NL","EN"], :label => false %> 
     </div> 
     <div class="col-md-8"> 
     <%= ff.input :keywords, placeholder: 'add keywords separated by " ; " - example: document number; document date' %> 
     </div> 
     <div class="col-md-1"> 
     <%= ff.check_box :_destroy, label: "del"%> 
     </div> 
     </div> 
    <% end -%> 
    </div> 
    <% end -%> 
+0

은 strongparams에': _destroy'를 추가하고 중첩 모델에 대한 유효성 검사를 고려해야 할 것으로 생각됩니다. –

답변

0

:

  • 중첩 기록을 파괴하는, 즉 (:id 외에)도 :_destroy
  • 중첩 된 필드에 대한 유효성 검사를 사용하는 strongparams에 추가되어 있는지 확인합니다 사용 accepts_nested_attributes_for :keywords, allow_destroy: true, reject_if: :something_to_check
관련 문제