RailsCasts Pro episodes are now free!

Learn more or hide this

Nic Feddema's Profile

GitHub User: nfeddema

Comments by Nic Feddema

Avatar

Figured it out, I was passing f instead of builder...

Avatar

For some reason :child_index isn't working for me in rails 3.0.10. My code is also modified to allow reordering. I am also using formtastic 1.2.4 with haml, and have tried switching the semantic_fields_for to fields_for and have also tried with or without the new_object as an array as a previous commenter noted. Here is the code:

application_helper.rb
def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.semantic_fields_for(association, [new_object], :index => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => f, :poll_answer => new_object)
    end
    link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')", :class => 'add')
  end
poll_answer_fields.html.haml
.field
  = f.semantic_fields_for :poll_answers, poll_answer do |builder|
    = builder.input :text
    = builder.hidden_field :position
    = builder.hidden_field :_destroy
    = link_to_function "Delete", "remove_fields(this)", :class => 'remove'
    %br