2011-09-18 3 views
1

저는 Ryan Bates 스크린 캐스트를 기반으로 복잡한 중첩 된 양식을 구현했습니다.여러 개의 중첩 된 양식

나는 같은 형태

하나가 고객의 약속이에 대한

의 다른 Cutomer의 접촉과 2 개 중첩 된 형태를 가지고 트루 잉하고있어 나는

환자 모델

class Patient < ActiveRecord::Base 
    has_many :contacts, :dependent => :destroy 
    accepts_nested_attributes_for :contacts, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true 

    has_many :appointments, :dependent => :destroy 
    accepts_nested_attributes_for :appointments, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true 
end 

연락처 모델

class Contact < ActiveRecord::Base 
    belongs_to :patient 
end 
01 23,516,

예약 모델

class Appointment < ActiveRecord::Base 
    belongs_to :patient 
end 

부분 형태

<div class="fields"> 
     Appointment: <%= f.datetime_select :appointment_date %> 
     <%= link_to_remove_fields "remove", f%><br /> 
</div> 

contact_fields 일부 형태를 부분적

<div class="fields"> 
     Contact: <%= f.text_field :contact_type %> 
     <%= f.text_field :content %> 
     <%= link_to_remove_fields "remove", f%><br /> 
</div> 

환자 _form을 appointment_fields

Edited to show only the formfields 

    <%= f.fields_for :contacts do |builder| %> 
     <%= render "contact_fields", :f => builder %> 
    <% end %> 

    <p><%= link_to_add_fields "Add More Contact", f, :contacts %></p> 

    <%= f.fields_for :appointments do |builder| %> 
     <%= render "appointment_fields", :f => builder %> 
    <% end %> 

    <p><%= link_to_add_fields "Add Appointment", f, :appointments %></p> 
01,235,

Application_helper

module ApplicationHelper 
    def link_to_remove_fields(name, f) 
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)") 
    end 

    def link_to_add_fields(name, f, association) 
    new_object = f.object.class.reflect_on_association(association).klass.new 
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| 
     render(association.to_s.singularize + "_fields", :f => builder) 
    end 
    link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')") 
     end 

end 

Application.js

function remove_fields(link) { 
    $(link).prev("input[type=hidden]").val("1"); 
    $(link).closest(".fields").hide(); 
} 

function add_fields(link, association, content) { 
    var new_id = new Date().getTime(); 
    var regexp = new RegExp("new_" + association, "g"); 
    $(link).parent().before(content.replace(regexp, new_id)); 
} 

을 나는 레일 3.0.3와 루비 1.9.2

마 contac이 작업을 수행하지만 약속은하지 않습니다를 사용하고 있습니다. 오류 메시지는 단지 내가 형태로 데이터를 입력하고 입력 할 때 콘솔 모드 아래 코드에서 할 경우 출력이

Started POST "/patients" for 127.0.0.1 at 2011-09-17 16:59:46 -0700 
    Processing by PatientsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"AEhUmtE5vIMrjHYWvGWRzlDc2hKrN0nc9gXPCSlIz50=", "patient"=>{"first_name"=>"test", "middle"=>"", "last_name"=>"", "dob(1i)"=>"2011", "dob(2i)"=>"9", "dob(3i)"=>"17", "address1"=>"", "address2"=>"", "city"=>"", "state"=>"", "country"=>"", "insurance_name"=>"", "contacts_attributes"=>{"0"=>{"contact_type"=>"cell", "content"=>"999-999-9999", "_destroy"=>"false"}, "1"=>{"contact_type"=>"", "content"=>"", "_destroy"=>"false"}}, "appointments_attributes"=>{"0"=>{"appointment_date(1i)"=>"2011", "appointment_date(2i)"=>"9", "appointment_date(3i)"=>"17", "appointment_date(4i)"=>"23", "appointment_date(5i)"=>"59", "_destroy"=>"false"}}, "notes"=>""}, "commit"=>"Create Patient"} 
    SQL (0.9ms) BEGIN 
    SQL (1.2ms) describe `patients` 
    AREL (1.5ms) INSERT INTO `patients` (`first_name`, `middle`, `last_name`, `dob`, `address1`, `address2`, `city`, `state`, `country`, `notes`, `insured`, `insurance_name`, `created_at`, `updated_at`) VALUES ('test', '', '', '2011-09-17 00:00:00', '', '', '', '', '', '', NULL, '', '2011-09-17 23:59:46', '2011-09-17 23:59:46') 
    SQL (3.2ms) describe `contacts` 
    AREL (0.3ms) INSERT INTO `contacts` (`patient_id`, `contact_type`, `content`, `created_at`, `updated_at`) VALUES (8, 'cell', '999-999-9999', '2011-09-17 23:59:46', '2011-09-17 23:59:46') 
    SQL (0.4ms) COMMIT 
Redirected to http://localhost:3000/patients/8 
Completed 302 Found in 74ms 

제출되는 문 여기

을 포함 실행하지 않습니다

@patients = Patient.find_by_id(1) 
@patients.appointment 

가 반환 [] 보여주는 관계는 오 접촉을 주석 시도했지만 여전히

01을 작동시킬 수 있었다

권리

어떤 아이디어 ??

답변

1

환자 모델에서 약속의 nested_attributes_for 선언에 존재하지 않는 "콘텐츠"속성에 대해 reject_if가 있습니다.

콘텐츠 속성은 약속의 매개 변수가 아니며 연락처의 매개 변수에 불과합니다. 거기에 대해 특성을 보내고 환자 모델에서 약속에 빈 콘텐츠 특성이 있다고 판단하면 약속의 중첩 특성을 거부합니다.그것을 해결하기 위해

는 appointement nested_attributes_for 선언

accepts_nested_attributes_for :appointments, :allow_destroy => true 

에서 reject_if 절을 제거하거나 기존 속성, 변경합니다. 약속 날짜와 같습니다.

이것이 문제인지 알려주세요.

+0

굉장 !! 고마워. 많이 ... 나는이 코드를 몇 번이나 읽었는지를 알지 못했고 그 숫자를 잃어 버렸다. 복사 및 붙여 넣기는 시간을 절약하는 대신에 방해가됩니다. 복사 및 붙여 넣기에서 여분의 시간을 보냈을 것이라고 쓰면 ... 대신 4 시간 동안 디버깅하려고했습니다. – Marrento

+0

도움이 되었으면 좋겠습니다. 도움을 요청하면 도움이되기 때문에 정말 좋은 출발입니다. 더 자주 시작하십시오. 환영합니다 :) – e3matheus

관련 문제