简体   繁体   中英

Rails Ajax partials passing form builder

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

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
When not relying on form_for , the form builder elements to be used are label_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 .

I answered the same question with my proposed solution here:

pass form builder in remote_function in rails?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM