3

我有一个应用程序以相同的形式使用 simple_form、cocoon、bootstrap-sass 和 select2。我已经使用 select2 完成了所有工作,并为第一个显示的对象设置了正确的样式,我相信这不是 cocoon 设置的。但是,我使用 link_to_add_fields 选项添加的任何对象都没有正确的内联样式或使用 select2。

第一个表单显示如下:

这是表单中第一个对象的格式

这是 link_to_add_fields 添加对象的方式:

在此处输入图像描述

我尝试使用回调在插入之前/之后调用 select2,但我无法让它工作。

希望有人能指出我正确的方向。

谢谢!

这是我的模型:

class Week < ActiveRecord::Base

  belongs_to :pool
  has_many   :games, dependent: :destroy

  accepts_nested_attributes_for :games

end
class Game < ActiveRecord::Base

  belongs_to :week

end

控制器代码:

  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

以下是表单视图:

新的.html.erb

<% provide(:title, "Create Week #{@week.weekNumber}") %>
<%= raw "<h1>Create Week #{@week.weekNumber}</h1>" %>
<%= simple_form_for([@pool, @week], html: { class: 'form-horizontal' }) do |f| %>
  <%= f.simple_fields_for :games do |g| %>
    <%= render 'game_fields', f: g %>
  <% end %>
  <%= link_to_add_association 'Add New Game', f, :games %><br /><br />
  <%= f.button :submit, label: "Create week",
               class: 'btn btn-large btn-primary' %>
<% end %>

_game_fields.html.erb

<div class='nested-fields well'>
  <fieldset>
    <%= 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' } %>
    <%= 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' } %>
    <%= f.input :_destroy, as: :hidden %>
    &nbsp;&nbsp;&nbsp;
    <%= link_to_remove_association "remove", f %>
  </fieldset>
</div>

这是我正在使用的javascript代码(或者至少在茧回调的情况下尝试使用):

/* 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
        });
      });
});
4

1 回答 1

5

您尝试在 上捕获回调#awayTeamIndex,但据我在您的代码中看到的,这是一个输入字段。您应该将回调放在包含插入元素的容器元素上。

例如写

$('form').on('cocoon:after-insert', function() {
  /* apply select2 styling */ 
});

为确保回调被击中,您始终可以添加一个alertor console.log

于 2013-09-18T22:44:58.590 回答