Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Cocoon link_to_add_association Renders Partial Twice

My partial gets rendered twice instead of only once, as expected. Any thoughts?

Here's my Person view

<%= simple_nested_form_for(@person) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">

    <%= f.input :name %>

    <h3>Records by year</h3>

    <div id='records'>
      <%= f.simple_fields_for :records do |record| %>
        <%= render 'record_fields', :f => record %>
      <% end %>

      <div class='links'>
        <%= link_to_add_association 'New Record', f, :records, :class => 'btn' %>
      </div>
    </div>

  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

Models (removed things such as constants and validations):

class Person < ActiveRecord::Base

    attr_accessible :name, :records_attributes

    has_many :records, :dependent => :destroy
    accepts_nested_attributes_for :records, :reject_if => :all_blank, :allow_destroy => true

end

class Record < ActiveRecord::Base

    attr_accessible :price, :status, :year, :person_id

    belongs_to :person
end

My _record_fields.html.erb partial looks like this:

<div class='nested-fields well'>
    <%= f.input :price %>

    <%= f.input :year %>

    <%= f.input :status, :collection => record_statuses, :include_blank => false %>

    <%= link_to_remove_association "Remove", f %>
</div>

An interesting issue is that, if I change where the partials are generated (so 'after' instead of the default 'before' the link_to_add_association link), it generates a partial after the button, but the duplicate is generated before the link_to_add_association link.

The only similar issues reported on here I could find were with caching in production. This is happening in development, and my caching is turned off (by default).

Am I missing something? Can anyone help?

Edit: Looking at the cocoon JavaScript, it seems the click event is called twice for one click (tested it with $('.add_fields').click(), which triggers $('.add_fields').live('click', function() {...} ) twice. I'm still at a loss as to why this might be happening. Input thoroughly appreciated.

Edit #2:

Controller:

  # GET /persons/new
  # GET /persons/new.json
  def new
    @person = Person.new
    # @person.records.build

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @person }
    end
  end

  # GET /persons/1/edit
  def edit
    @person = Person.find(params[:id])
  end
like image 453
roobeedeedada Avatar asked Jan 23 '13 14:01

roobeedeedada


1 Answers

I was having this same issue. My problem was I requiring the cocoon javascript twice.

Once in my application.js

// app/assets/javascripts/application.js
//= require cocoon

and once in my application layout

/ app/views/layouts/application.html.haml
= javascript_include_tag :cocoon

after removing the include tag from my layout it began working as expected

like image 73
Thomas Avatar answered Sep 25 '22 05:09

Thomas