title: Can't get select2 to work when using simple_form and cocoon
body: <p>I've got an application that is using simple_form, cocoon, bootstrap-sass, and select2 all in the same form.  I've got everything working with select2 and styled correctly for the first object that gets displayed, which, I believe, is not setup by cocoon. However, any objects that I add with the link_to_add_fields option have neither the correct in-line styling or use select2.</p>
  
  <p>The first form shows up like the following:</p>
  
  <p><img src="https://i.stack.imgur.com/E15bL.png" alt="This is the format for first object in form"></p>
  
  <p>This is how the link_to_add_fields is adding the object:</p>
  
  <p><img src="https://i.stack.imgur.com/66jHO.png" alt="enter image description here"></p>
  
  <p>I've tried using the callbacks to call select2 on the before/after insert and I couldn't get it to work.  </p>
  
  <p>Hopefully, someone can point me in the right direction.</p>
  
  <p>Thanks!</p>
  
  <p>Here are my models:</p>
  
  <pre><code>class Week &lt; ActiveRecord::Base
  
    belongs_to :pool
    has_many   :games, dependent: :destroy
  
    accepts_nested_attributes_for :games
  
  end
  class Game &lt; ActiveRecord::Base
  
    belongs_to :week
  
  end
  </code></pre>
  
  <p>Controller code:</p>
  
  <pre><code>  def new
      @pool = Pool.find(params[:pool_id])
      if !@pool.nil?
        @week = @pool.weeks.new
        @game = @week.games.build
        @week.weekNumber = @pool.weeks.count + 1
      else
        flash[:error] = "Cannot create week. Pool with id:#{params[:pool_id]} does not exist!"
        redirect_to pools_path
      end
    end
  
    def create
      @pool = Pool.find(params[:pool_id])
      @week = @pool.weeks.new(week_params)
      @week.weekNumber = @pool.weeks.count + 1
      if @week.save
        # Handle a successful save
        flash[:success] = 
            "Week #{@week.weekNumber} for '#{@pool.name}' was created successfully!"
        # Set the state to Pend
        @week.setState(Week::STATES[:Pend])
        redirect_to @pool
      else
        render 'new'
      end
    end
  </code></pre>
  
  <p>Here are the form views:</p>
  
  <p>new.html.erb</p>
  
  <pre><code>&lt;% provide(:title, "Create Week #{@week.weekNumber}") %&gt;
  &lt;%= raw "&lt;h1&gt;Create Week #{@week.weekNumber}&lt;/h1&gt;" %&gt;
  &lt;%= simple_form_for([@pool, @week], html: { class: 'form-horizontal' }) do |f| %&gt;
    &lt;%= f.simple_fields_for :games do |g| %&gt;
      &lt;%= render 'game_fields', f: g %&gt;
    &lt;% end %&gt;
    &lt;%= link_to_add_association 'Add New Game', f, :games %&gt;&lt;br /&gt;&lt;br /&gt;
    &lt;%= f.button :submit, label: "Create week",
                 class: 'btn btn-large btn-primary' %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>_game_fields.html.erb</p>
  
  <pre><code>&lt;div class='nested-fields well'&gt;
    &lt;fieldset&gt;
      &lt;%= f.input :awayTeamIndex, label: false,
                      collection: NflTeam.all, value_method: :id,
                      label_method: :name,
                      placeholder: "Away Team",
                      include_blank: true, 
                      input_html: { id: "awayteam", class: 'span3' } %&gt;
      &lt;%= f.input :homeTeamIndex, label: false,
                      collection: NflTeam.all, value_method: :id,
                      label_method: :name,
                      placeholder: "Home Team",
                      include_blank: true,
                      input_html: { id: "hometeam", class: 'span3' } %&gt;
      &lt;%= f.input :_destroy, as: :hidden %&gt;
      &amp;nbsp;&amp;nbsp;&amp;nbsp;
      &lt;%= link_to_remove_association "remove", f %&gt;
    &lt;/fieldset&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And here is the javascript code I'm using (or at least trying to use in the case of the cocoon callbacks) :</p>
  
  <pre><code>/* Setup the select2 functions */
  $(document).ready(function() {
  
    $('select#hometeam').select2({
      placeholder: "Home Team",
      allowClear: true
    });
  
    $('select#awayteam').select2({
      placeholder: "Away Team",
      allowClear: true
    });
  
    $('select#pick').select2();
  });
  
  /* setup cocoon nested forms insertion mode */
  $(document).ready(function() {
    $("a.add_fields").
      data("association-insertion-position", 'before').
      data("association-insertion-node", 'this');
  });
  
  $(document).ready(function() {
    $('#awayTeamIndex')
        .on('cocoon:after-insert', function() {
          /* ... do something ... */
          $('select#awayteam').select2({
            placeholder: "Away Team",
            allowClear: true
          });
        });
  });
  </code></pre>