title: rails link_to_add_fields not adding fields with has_many :through (with nested form inside)
body: <p>I have the link_to_add_fields code working for many other models, but here it is not working.  I use Ryan Bates screen casts to model how I do mine.</p>
  
  <p>What might different with this is that I have 2 nested models in one partial. Half the table fields are from model a (journals), and the other are from model b (journal_entries) and all are based off a Leases model.</p>
  
  <p>The table looks like the following, [xx] are input fields. It looks kinda like a data grid.</p>
  
  <pre><code>Dated      |Memo      |Account           |Credit     |
  -----------------------------------------------------------
  [a.dated]  |[a.memo]  |[b.account_name]  |[b.credit] |....
  [a.dated]  |[a.memo]  |[b.account_name]  |[b.credit] |....
  [a.dated]  |[a.memo]  |[b.account_name]  |[b.credit] |....
  (Link:Click to add another row)
  </code></pre>
  
  <p>When I inspect the link_to_add_fields in the produced html is see:</p>
  
  <pre><code>&lt;a href="#" class="add_fields" data-fields="&amp;lt;fieldset style=&amp;quot;
  border:0px none;padding:0px .625em;&amp;quot;&amp;gt;  &amp;lt;/fieldset&amp;gt;" 
  data-id="-629772378"&gt;+ Add transactions&lt;/a&gt;
  </code></pre>
  
  <p>I expected a lot more action than that... Obviously when I click the link, nothing happens.  </p>
  
  <p>_journals_form.html.erb</p>
  
  <pre><code>&lt;div&gt;
    &lt;%= f.fields_for :journals do |journals| %&gt;
      &lt;%= render "journal_fields" , {f: journals} %&gt;
    &lt;% end %&gt;
    &lt;%= link_to_add_fields "+ Add transactions", f, :journals %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>_journal_fields.html.erb, Notice the nested f.fields_for and the mix of f.attribute and g.attribute</p>
  
  <pre><code>&lt;fieldset style="border:0px none;padding:0px .625em;"&gt;
    &lt;%= f.fields_for :journal_entries do |g| %&gt; 
        &lt;table&gt;
          &lt;tr&gt;
            &lt;td style="width:200px;font-size:.7em"&gt;&lt;%= f.date_select :dated %&gt;&lt;/td&gt;
            ...
            &lt;td style="width:100px;"&gt;&lt;%= g.text_field :account_name, size: 8 %&gt;&lt;/td&gt;
            &lt;td style="width:100px;"&gt;&lt;%= g.hidden_field :_destroy %&gt;&lt;%= link_to "remove", '#', class: "remove_fields" %&gt;&lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;
    &lt;% end %&gt;
  &lt;/fieldset&gt;
  </code></pre>
  
  <p>My controller appears to edit/update and add rows of both models (journals and journal_entries) except that the remove link is buggy...</p>
  
  <pre><code>  def edit
      @lease = Lease.find(params[:id])
      @lease.tenants.build
      1.times do 
        journal = @lease.journals.build
        journal.journal_entries.build
      end
    end
    def update
      @lease = Lease.find(params[:id])
      if @lease.update_attributes(params[:lease])
        redirect_to edit_pm_lease_path(@lease), notice: 'Lease was successfully updated.'
      else
        render action: "edit"
      end
    end
  </code></pre>
  
  <p>The model is based off Leases.  Leases have many journals and many journal entries through journals.  This is how we keep track of payments and bills for a given lease.  Leases also has many tenants (and this add_fields... works great).</p>
  
  <p>Lease.rb</p>
  
  <pre><code>class Lease &lt; ActiveRecord::Base
    ...
    has_many :tenants
    has_many :journals, :order =&gt; [:dated, :id] #, :conditions =&gt; "journals.lease_id = id"
    has_many :journal_entries, :through =&gt; :journals  
    accepts_nested_attributes_for :tenants , :reject_if =&gt; lambda { |a| a[:name].blank? }, :allow_destroy =&gt; true 
    accepts_nested_attributes_for :journal_entries , :allow_destroy =&gt; true
    accepts_nested_attributes_for :journals ,  :allow_destroy =&gt; true    
    #Below didn't seem to help or hinder so it's removed.
      #accepts_nested_attributes_for :journals, :journal_entries, :allow_destroy =&gt; true 
    ...
  end
  </code></pre>
  
  <p>journal.rb</p>
  
  <pre><code>class Journal &lt; ActiveRecord::Base
    ... 
    belongs_to :lease, :conditions =&gt;  :lease_id != nil   
    has_many :journal_entries
    accepts_nested_attributes_for :journal_entries , :allow_destroy =&gt; true
  end
  </code></pre>
  
  <p>journal_entry.rb</p>
  
  <pre><code>class JournalEntry &lt; ActiveRecord::Base
    belongs_to :journal
  end
  </code></pre>
  
  <p>link_to_add_fields helper: (References from Ryan bates railscasts (EPISODE #196)</p>
  
  <pre><code>module ApplicationHelper
    def external_link_to(label, target, options = [])
      #length = 25 #options[:length] ||= 25
      #window = options[:target] ||= "new"
      unless target.downcase.start_with?("http://","https://")
        link = "http://" + target.strip
      end
      link_to target, link, :target =&gt; "new"   
    end
  
    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 |builder|
        render(association.to_s.singularize + "_fields", f: builder)
      end
      link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
    end
  end
  </code></pre>
  
  <p>Not sure where I went wrong with this one, the adding and removing of the data are working, but the link_to_add_fields for only the journal/journal_entry form is not responding as it should.  I'm not using the nested_form gem, if that makes a difference...</p>
  
  <p>I'm using Rails 3.2.12, Ruby 1.9.3</p>
  
  <p>Any help would be greatly appreciated.</p>