【发布时间】:2016-03-23 16:14:16
【问题描述】:
我的 Cocoon gem 可以很好地添加和删除不同深度的嵌套字段。我还有after-insert 和after-remove 回调在使用id 的第一级嵌套中工作。我无法使用我正在使用的类名在第二级触发它,因为会有多组此嵌套字段集。
views/car_buyers/_form.html.haml
...
#cars
= f.simple_fields_for :cars do |c|
= render 'car_fields', f: c
.link-add
= link_to_add_association ' + Add a Car', f, :cars, partial: 'car_fields'
...
views/car_buyers/_car_fields.html.haml
.nested-fields.car
...
.upgrade-options
= f.simple_fields_for :upgrade_options, do |uo|
= render 'upgrade_option_fields', f: uo
.link-add
= link_to_add_association ' + Add an Upgrade Option', f, :upgrade_options, partial: 'upgrade_option_fields'
...
views/car_buyers/_upgrade_option_fields.html.haml
.nested-fields.upgrade-option
= f.input :upgrade_option_type, label: 'Upgrade Option',
collection: @upgrade_options_list,
include_blank: 'Select...',
input_html: { class: 'upgrade-option-type-select input-upgrade-option' },
error: 'Upgrade Option selection required.'
= f.input :upgrade_option_value, label: 'Upgrade Option Value',
collection: @upgrade_option_values_list,
include_blank: 'Select...',
input_html: { class: 'upgrade-option-value-select input-upgrade-option' },
error: 'Upgrade Option Value selection required.'
.link-remove
= link_to_remove_association icon('remove'), f, class: 'upgrade-option-remove-link remove-link'
assets/javascripts/car_buyers.js
$(document).ready(function() {
...
$('#cars').on('cocoon:after-insert', function() {
// this is working
...
}).on('cocoon:after-remove', function() {
// this is also working
...
});
...
$('.upgrade-options').on('cocoon:before-insert', function() {
// this is NOT working
...
});
...
});
不使用 turbolinks,如果这很重要的话。仔细检查标记以确保 id/classes 是正确的。看来我缺少一些基本的东西。
谢谢。
【问题讨论】:
标签: jquery ruby-on-rails-4 callback cocoon-gem