予想どおり、パーシャルが 1 回だけではなく 2 回レンダリングされます。何かご意見は?
これが私のPersonビューです
<%= simple_nested_form_for(@person) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<h3>Records by year</h3>
<div id='records'>
<%= f.simple_fields_for :records do |record| %>
<%= render 'record_fields', :f => record %>
<% end %>
<div class='links'>
<%= link_to_add_association 'New Record', f, :records, :class => 'btn' %>
</div>
</div>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
モデル (定数や検証などを削除):
class Person < ActiveRecord::Base
attr_accessible :name, :records_attributes
has_many :records, :dependent => :destroy
accepts_nested_attributes_for :records, :reject_if => :all_blank, :allow_destroy => true
end
class Record < ActiveRecord::Base
attr_accessible :price, :status, :year, :person_id
belongs_to :person
end
私の _record_fields.html.erb パーシャルは次のようになります。
<div class='nested-fields well'>
<%= f.input :price %>
<%= f.input :year %>
<%= f.input :status, :collection => record_statuses, :include_blank => false %>
<%= link_to_remove_association "Remove", f %>
</div>
興味深い問題は、パーシャルが生成される場所を変更すると (link_to_add_association リンクのデフォルトの「前」ではなく「後」)、ボタンの後にパーシャルが生成されますが、重複は link_to_add_association リンクの前に生成されることです。
ここで報告された唯一の同様の問題は、本番環境でのキャッシュに関するものでした。これは開発中に発生しており、私のキャッシュは (デフォルトで) オフになっています。
何か不足していますか?誰でも助けることができますか?
編集: コクーン JavaScript を見ると、1 回のクリックに対してクリック イベントが 2 回呼び出されているようです ($('.add_fields').click() でテストすると、$('.add_fields').live('click' がトリガーされます) , function() {...} ) を 2 回入力しました.なぜこれが起こっているのか、私はまだ途方に暮れています.入力は非常に感謝しています.
編集#2:
コントローラ:
# GET /persons/new
# GET /persons/new.json
def new
@person = Person.new
# @person.records.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @person }
end
end
# GET /persons/1/edit
def edit
@person = Person.find(params[:id])
end