Rails 3 + Ajax: how to access my local form builder instance

前端 未结 2 1076
南笙
南笙 2021-02-04 10:49

I have a form that displays a set of inputs. I also have a button, and when clicked, I make an ajax request which is supposed to replace the existing inputs with a different set

2条回答
  •  别跟我提以往
    2021-02-04 11:52

    Many thanks to @flyfish, his answer helped me solved how to do ajax with nested attributes. I took @flynfish answer and tweeked it for my situation:

    <%= fields_for object do |f| %>
      <%= f.fields nested_object, child_index: Time.now.to_i do |builder| %>
    <% end %>
    

    $('#info').html("<%= escape_javascript(render 'my_second_fields_partial', object: @object), nested_object: @nested_object %>

    The child_index is important for without it your params hash will build with [0] and will then overlay any other records built with the initial fields_for which starts at zero and increments from there.

提交回复
热议问题