【发布时间】:2013-01-23 14:24:37
【问题描述】:
正如预期的那样,我的部分被渲染了两次而不是只渲染一次。有什么想法吗?
这是我的个人视图
<%= 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,似乎单击事件被调用两次(用$('.add_fields').click()测试它,触发$('.add_fields').live('click',函数() {...} ) 两次。我仍然不知道为什么会发生这种情况。非常感谢输入。
编辑#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
【问题讨论】:
-
我们可以看看您的控制器操作代码吗?
-
我意识到在回家的路上我忽略了它。道歉并补充。
-
渲染整个页面的时候还有其他的内联js吗?并尝试使用 form_for 和 fields_for
-
我已经尝试删除所有 JS(包括我在 application.js 中的所有内容)。它没有帮助。我只用 form_for 和 fields_for 对其进行了测试,但这似乎也没有任何作用。
标签: ruby-on-rails-3 nested-attributes cocoon-gem