Reputation: 1185
I am loading part of my form with ajax depending on the value of a dropdown as follows:
..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 %>
It is called here...
.._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>
I then can load in the files with that, and it works great the only problem being I need to pass the form builder to the partial as my _buy.html.erb looks like this
<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>
Which obviously needs the form builder, I have tried passing it through as a local etc. but to no avail. I have also attempted removing all the "f." this works, but the form is not displayed correctly, and none of the fancier form builder elements work..
How can I get the form builder to the partial?
Thanks
Upvotes: 1
Views: 2445
Reputation: 3988
I answered the same question with my proposed solution here:
pass form builder in remote_function in rails?
Upvotes: 1
Reputation: 777
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
When not relying on form_for
, the form builder elements to be used arelabel_tag
and select_tag
respectively.
You may find it easier to use both form_tag
and fields_for
in this case, rather than form_for
.
Upvotes: 1