我下面瑞安貝茨neted_forms插曲1 & 2從軌道蒙上,我已經實現了嵌套表格的功能在我的項目之一&之前,它的工作罰款沒有任何錯誤,但在我的新項目中,我使用相同的參考從軌道鑄件,但刪除&添加字段無法正常工作。remove_fields和添加嵌套表上無法工作在軌字段鏈接3.0.9
這裏是我的模型
has_many :contacts, :dependent => :destroy
accepts_nested_attributes_for :contacts, :reject_if => lambda { |a| a[:contact_name].blank? }, :allow_destroy => true
形式上我使用添加字段鏈接
<div class="TabbedPanelsContent">
<%= f.fields_for :contacts do |builder| %>
<%= render "contact_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add Contact", f, :contacts %></p>
</div>
局部聯繫人
<div class="fields">
<p class="lable">Contact Name</p>
<p class="field"><%= f.text_field :contact_name %></p></br>
<p class="lable">Mobile Number</p>
<p class="field"><%= f.text_field :contact_mobile_number %></p></br>
<p class="lable">Email Address</p>
<p class="field"><%= f.text_field :contact_email %></p></br>
<p><%= link_to_remove_fields "remove", f %></p> </div>
的應用助手我寫了這個方法
# Method for removing fields
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
# Method for Adding fields
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
的的application.js是如下
function remove_fields(link) {
$(link).previous("input[type=hidden]").value = "1";
$(link).up(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).up().insert({
before: content.replace(regexp, new_id)
});
}
使用螢火蟲我已發現在HTML一個問題,同時執行此代碼
<p><input type="hidden" value="false" name="customer[contacts_attributes][1][_destroy]" id="customer_contacts_attributes_1__destroy"><a onclick="remove_fields(this); return false;" href="#">remove</a></p>
在上面的代碼的值這是越來越產生= 「假」可能會產生問題&它應該是值=「1」,因爲我以前的代碼正在生成值=「1」。有誰能幫我解決上述問題嗎?