I'm new to rails. I appreciate any help. I followed railscast episode 196. I created a nested form that is working fine and saving to the database. I want to add field dynamically but the new added fields are not saving to the database.
Here is the helper method I created: application_helper.rb
module ApplicationHelper
  def link_to_add_fields(name, f, association)
    new_object = f.object.send(association).klass.new
    id = new_object.object_id
    fields = f.fields_for(association, new_object, child_index: id) do |d|
      render(association.to_s.singularize + "_fields", :f => d)
    end
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("/n", "")})
  end
end
_form.html.erb
 <%= form_for(@track, html: { multipart: true}) do |f| %>
      <%= f.label :name %>
      <%= f.text_field :name, class: "form-control"%>
      <%= f.label :category %>
      <%= f.text_field :category, class: "form-control" %>
      <div class="form-group picture">
        <%= f.label :picture %>
        <%= f.file_field :picture, class: 'form-control', accept: 'image/jpeg,image/gif,image/png' %>
      </div>
      <%= f.label :description %>
      <%= f.text_field :description, class: "form-control" %>
      <%= f.label :tags %>
      <%= f.text_area :tags, class: "form-control" %>
<div class="row">
  <div class="well col-md-8 col-md-offset-2">
      <%= f.fields_for :docs do |d| %>
          <%= render 'doc_fields', f: d %>
      <% end %>
      <%= link_to_add_fields("Add Lesson", f, :docs) %>
  </div>
</div>
<%= f.submit(@track.new_record? ? 'Publish Track' : 'Update Track', class: "btn btn-success pull-right") if user_signed_in? %>
  <% end %>
tracks.js.coffee
jQuery ->
  $('.remove_fields').click ->
    $(this).prev('input[type=hidden]').val('1')
    $(this).closest('fieldset').hide()
    event.preventDefault()
  $('.add_fields').click ->
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    event.preventDefault()
track.rb
class Track < ActiveRecord::Base
  # model associations
  belongs_to :user
  has_many :docs
  # nested model
  accepts_nested_attributes_for :docs, allow_destroy: true
end
track_controller.rb
 def new
    @track = Track.new
    @track.docs.build
  end
  def create
    @track = Track.new(track_params)
    @track.user = current_user
    if @track.save
      flash[:success] = "Your track was created succesfully!"
      redirect_to track_path(@track)
    else
      render :new
    end
  end
 def track_params
      params.require(:track).permit(:name, :category, :description, :tag, :picture,
                                :docs_attributes => [:id, :title, :format, :_destroy])
    end
The remove is working fine. The add isnt saving to the database. I appreciate your help!
 
    