我正在使用 ajax 加载表单的一部分,具体取决于下拉列表的值,如下所示:
..form_partial.js.erb
$('#custom_ajax').remove();
<% if @house == "Rent" %>
$('#rest_of_form').after('<div id="custom_ajax"><%= escape_javascript render('/houses/forms/rent') %></div>');
<% else %>
$('#rest_of_form').after('<div id="custom_ajax"><%= escape_javascript render('/houses/forms/buy') %></div>');
<% end %>
这里叫...
.._form.html.erb
<%= form_for @house, :validate => true, :html => { :class => 'form-horizontal',:multipart => true } do |f| %>
..........................................
..........................................
<div id = "rest_of_form"></div>
<div id="custom_ajax">
..........................................
..........................................
</div>
..........................................
..........................................
<script type="text/javascript">
$(document).ready(function() {
$('#house_listing_type').change(function() {
$.ajax({ url: '/houses/' + this.value + '/form_partial' });
});
});
</script>
然后我可以用它加载文件,它工作得很好,唯一的问题是我需要将表单构建器传递给部分,因为我的 _buy.html.erb 看起来像这样
<div class="control-group">
<%= label :property_classification, "Property Classification", :class => "control-label" %>
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-info-sign"></i></span>
<%= select :property_classification, ["Residential","Commercial"], {},{:class=>"input-medium"} %>
</div>
</div>
</div>
这显然需要表单生成器,我尝试将其作为本地等传递,但无济于事。我还尝试删除所有“f”。这行得通,但是表单显示不正确,并且更高级的表单构建器元素都不起作用..
我怎样才能让表单生成器部分生成?
谢谢