私はnil
としてchild_index
を設定する(Itay Grudev's blog postに基づいて)非常に類似した方法を、持っていた後、(多くのレコードが同じid
を持つそれらを避けて追加することができるように)タイムスタンプでそれを置き換えるためにjQueryのを使用します。だから、
、私が持っている私のapplication_helper.rb
ファイル内:
def link_to_add_fields(name = nil, f = nil, association = nil, options = nil, html_options = nil, &block)
f, association, options, html_options = name, f, association, options if block_given?
options = {} if options.nil?
html_options = {} if html_options.nil?
if options.include? :locals
locals = options[:locals]
else
locals = { }
end
if options.include? :partial
partial = options[:partial]
else
partial = association.to_s.singularize + '_fields'
end
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, child_index: nil) do |builder|
render(partial, locals.merge!(fields: builder))
end
html_options['data-form-prepend'] = raw CGI::escapeHTML(fields)
html_options['href'] = '#'
content_tag(:a, name, html_options, &block)
エンド
そして私は自分の名前を置き換え、私は新しく追加されたフィールドを見つけているdynamic_tables.js.coffee
と呼ばれるCoffeeScriptのファイルを追加しましたタイムスタンプ(それらはすべてグループとしてインデックス付けされているので)、ターゲット(テーブルの一番下に追加した隠し要素で、prepend_target
とマークしたもの)にフォームを追加します。だから私は持っている:
$('[data-form-prepend]').click (e) ->
target = $($(this).attr('prepend_target'))
obj = $($(this).attr('data-form-prepend'))
current_time = (new Date).getTime()
obj.find('input, select, textarea').each ->
$(this).attr 'name', ->
$(this).attr('name').replace 0, current_time
return
obj.insertBefore target
false
それはトリックを行う必要があります。お役に立てれば。