我有一个使用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
以下是表单视图:
new.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 %>
<%= link_to_remove_association "remove", f %>
</fieldset>
</div>
这是我正在使用的javascript代码(或者至少尝试在cocoon回调的情况下使用):
/* 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
});
});
});
答案 0 :(得分:4)
您尝试捕获#awayTeamIndex
上的回调,但就我在代码中看到的那样,这是一个输入字段。您应该将回调放在包含插入元素的容器元素上。
E.g。写
$('form').on('cocoon:after-insert', function() {
/* apply select2 styling */
});
要确保回调被点击,您随时可以添加alert
或console.log
。