awesome_nested_fields gem nested form not showing

2019-07-24 23:15发布

问题:

I have been having trouble with nested forms -- so I attempted to use the following gem: https://github.com/lailsonbm/awesome_nested_fields. Unfortunately the nested form (attitude) is not showing up in the task form -- I tried to follow the instructions exactly -- and I am hoping it is something dumb. I do not get any errors when the form is loaded.

task model

class Task < ActiveRecord::Base
has_many :attitudes
accepts_nested_attributes_for :attitudes, allow_destroy: true
attr_accessible :attitudes_attributes
end

attitude model

class Attitude < ActiveRecord::Base
belongs_to :task
accepts_nested_attributes_for :task
attr_accessible :tasks_attributes

end

task form

<%= form_for @task do |f| %>
<%= f.error_messages %>
<div class="items">
<%= f.nested_fields_for :attitudes do |f| %>
<fieldset class="item">
<%= f.label :name %>
<%= f.text_field :name %>
<a href="#" class="remove">remove</a>
<%= f.hidden_field :id %>
<%= f.hidden_field :_destroy %>
</fieldset>
<% end %>
</div>
<a href="#" class="add">Add Attitudes</a>
<% end %>

when I view the html source I see this

<script class="template item attitude" type="text/html">
&lt;fieldset class="item"&gt;
&lt;label for="task_attitudes_attributes_new_nested_item_name"&gt;Name&lt;/label&gt;
&lt;input id="task_attitudes_attributes_new_nested_item_name" name="task[attitudes_attributes][new_nested_item][name]" size="30" type="text" /&gt;
&lt;a href="#" class="remove"&gt;remove&lt;/a&gt;
&lt;input id="task_attitudes_attributes_new_nested_item_id" name="task[attitudes_attributes][new_nested_item][id]" type="hidden" /&gt;
&lt;input id="task_attitudes_attributes_new_nested_item__destroy" name="task[attitudes_attributes][new_nested_item][_destroy]" type="hidden" value="false" /&gt;
&lt;/fieldset&gt;
</script>

task form header

<script src="/javascripts/jquery.js?1385786590" type="text/javascript"></script>
<script src="/javascripts/jquery_ujs.js?1385786591" type="text/javascript"></script>
<script src="/javascripts/jquery.nested-fields.js?1385787505" type="text/javascript"></script>
<script src="/javascripts/application.js?1385790193" type="text/javascript"></script>
<script src="/javascripts/bootstrap.js?1385522548" type="text/javascript"></script>

application.js

$(document).ready(function(e) {
$('FORM').nestedFields();
});

回答1:

For anyone who wants to know -- I finally realized -- my problem was this ---

<%= f.hidden_field :id %>

When I I removed it -- all functioned properly and the information processed.