2011-09-18 14 views
1

私はRyan Batesのスクリーン・キャストに基づいて複雑なネストされたフォームを実装しました。複数のネストされたフォーム

私は同じ形

1で2つのネストされたフォームを持つようにツルーイングてるお客様の予定そのため

ためCutomerの連絡先やその他のために私は

患者モデルを持っている

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 

予定モデル

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を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> 
部分

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

患者_form

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とRuby 1.9.2

contacが作業を行いますが、予定はないを使用しています。エラー・メッセージがちょうど含ま声明ここ

を実行しません、私は、フォームにデータを入力し、私は、コンソールモード

@patients = Patient.find_by_id(1) 
@patients.appointment 
次のコードで行う場合

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 

を提出入力したときに出力されます

それは関係が右

ああ、私は接触をコメントアウトしようとしたが、まだそれが

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

私は助けてくれるとうれしいです.Btw、助けを求めるのは本当に良いスタートです。もっと頻繁にやり直してください。と歓迎:) – e3matheus

関連する問題