我正在尝试创建一个表单,其中嵌套属性是在新选项卡中生成的。我已经设置了我的模型以便它接受嵌套属性,但是当我提交表单时,我为文本字段设置的任何值都作为空白传递,但控制器将嵌套字段传递给模型。
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x", "group"=>{"pilgrims_attributes"=>{"1399738268141"=>{"surname"=>"", "name"=>"", "middle"=>"", "aka"=>"", "prefix"=>"", "suffix"=>"", "nationality"=>"", "passport_number"=>"", "expiration(2i)"=>"5", "expiration(3i)"=>"10", "expiration(1i)"=>"2014", "date_of_birth(2i)"=>"5", "date_of_birth(3i)"=>"10", "date_of_birth(1i)"=>"2014", "accommodation"=>"", "roommate_id"=>"", "flight"=>"", "seat_id"=>"", "special_diet"=>"", "status"=>"Pilgrim"}}, "email"=>"", "telephone"=>"", "secondary_telephone"=>"", "address"=>"", "country_code"=>"", "state_code"=>"", "city"=>"", "zip"=>""}, "commit"=>"Create Group", "trip_id"=>"1"}
所以它似乎认为它需要提交字段,并且正确地传递date_selects中的默认值,但是我在文本字段中输入的值只是空白。
所以目前我没有使用fields_for块,但似乎我不需要因为某些东西被传递给控制器。如果我确实需要使用fields_for
块,我应该将它放在html标记中吗?
守则:
我的表格:
<%= simple_form_for [@trip, @group] do |f| %>
<ul id="infoTabs" class="nav nav-tabs">
<li class="active"><a href="#pilgrim_info" data-toggle="tab">Pilgrims</a></li>
<li><a href="#contact" data-toggle="tab">Contact Info</a></li>
<li><a href="#payments" data-toggle="tab">Payments</a></li>
</ul>
<div class="tab-content">
<div id="pilgrim_info" class="tab-pane active">
<ul id="pilgrimTabs" class="nav nav-pills">
<li><%= link_to_add_pilgrim_fields('+ Add Pilgrim', f, :pilgrims, :data => {:toggle => 'pill'}, :class => 'add-pilgrim') %></li>
</ul>
<div class="tab-content">
</div>
</div>
</div>
<div class="row-fluid">
<div class="actions span12">
<%= f.submit :class => 'btn btn-primary' %>
<%= link_to 'Cancel', trip_groups_path(@trip), :class => "btn" %>
</div>
</div>
这是我点击添加朝圣者按钮后呈现的html片段:
<input id="group_pilgrims_attributes_1399739023635_surname" name="group[pilgrims_attributes][1399739023635][surname]" placeholder="Last Name" size="30" type="text">
link_to帮助
def link_to_add_pilgrim_fields(name, f, association, options = {})
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s , :f => builder)
end
link_to_function(name, "add_pilgrims_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")", options)
end
添加字段的javascript函数:
function add_pilgrims_fields(link, association, content) {
var new_id = new Date().getTime();
var regex = new RegExp("new_" + association, "g");
var display_id = $(".nav-pills").children().length;
$('.add-pilgrim').closest('li').after('<li><a href="#pilgrim'+ new_id +'" data-toggle="pill">New Pilgrim ' + display_id + '</a><span>x</span></li>');
$('.tab-content').append('<div class="tab-pane" id="pilgrim'+new_id+'">' + content.replace(regex, new_id) + '</div>');
}
答案 0 :(得分:0)
Javascript函数添加了一些额外的html。因为我有两个带有“.tab-content”类的元素,所以它将它附加到两个标签区域。当我提交表单时,它提交了空白表单,因为生成的两个选项卡都具有相同的ID。
我改变了这一行
$('.tab-content').append('<div class="tab-pane" id="pilgrim'+new_id+'">' + content.replace(regex, new_id) + '</div>');
}
到
$('traveller_form_tabs')。append(''+ content.replace(regex,new_id)+''); }
并在标签
中添加了ID<div id="pilgrim_info" class="tab-pane active">
<ul id="pilgrimTabs" class="nav nav-pills">
<li><%= link_to_add_pilgrim_fields('+ Add Pilgrim', f, :pilgrims, :data => {:toggle => 'pill'}, :class => 'add-pilgrim') %></li>
</ul>
<div class="tab-content" id="traveller_form_tabs">
</div>
现在正在提交我在字段中输入的值。