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

前端 未结 2 1075
南笙
南笙 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:30

    Can you use fields_for in your partial, and pass the @object to it? That way you don't need to pass a form builder?

    partial:

    <%= fields_for object do |f| %>
      f.text_field :field_name
    <% end %>
    


    $('#info').html("<%= escape_javascript(render 'my_second_fields_partial', object: @object) %>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题