awesome_nested_fields gem嵌套窗体不显示 [英] awesome_nested_fields gem nested form not showing
问题描述
我一直遇到嵌套表单的问题 - 所以我试图使用以下gem: https:// github的.com / lailsonbm / awesome_nested_fields 。不幸的是,嵌套表格(态度)没有出现在任务表单中 - 我试图完全按照说明操作 - 我希望这是一种愚蠢的行为。
任务模型
class Task< ActiveRecord :: Base
has_many:态度
accept_nested_attributes_for:attitudes,allow_destroy:true
attr_accessible:attitudes_attributes
end
态度模型
class Attitude< ActiveRecord :: Base
belongs_to:任务
accepts_nested_attributes_for:任务
attr_accessible:tasks_attributes
end
任务表单
<%= form_for @task do | f | %GT;
<%= f.error_messages%>
< div class =items>
<%= f.nested_fields_for:态度do | f | %GT;
< fieldset class =item>
<%= f.label:name%>
<%= f.text_field:name%>
< a href =#class =remove>删除< / a>
<%= f.hidden_field:id%>
<%= f.hidden_field:_destroy%>
< / fieldset>
<%end%>
< / div>
< a href =#class =add>添加态度< / a>
<%end%>
当我查看html源代码时,我看到这个
< script class =template item attitudetype =text / html>
& lt; fieldset class =item& gt;
& lt; label for =task_attitudes_attributes_new_nested_item_name& gt;名称& lt; / label& gt;
& lt; input id =task_attitudes_attributes_new_nested_item_namename =task [attitudes_attributes] [new_nested_item] [name]size =30type =text/& gt;
& lt; a href =#class =remove& gt; remove& lt; / a& gt;
& lt; input id =task_attitudes_attributes_new_nested_item_idname =task [attitudes_attributes] [new_nested_item] [id]type =hidden/& gt;
& lt; input id =task_attitudes_attributes_new_nested_item__destroyname =task [attitudes_attributes] [new_nested_item] [_ destroy]type =hiddenvalue =false/& gt;
& lt; / fieldset& gt;
< / script>
任务表单标题
< script src =/ javascripts / jquery.js?1385786590type =text / javascript>< / script>
< script src =/ javascripts / jquery_ujs.js?1385786591type =text / javascript>< / script>
< script src =/ javascripts / jquery.nested-fields.js?1385787505type =text / javascript>< / script>
< script src =/ javascripts / application.js?1385790193type =text / javascript>< / script>
< script src =/ javascripts / bootstrap.js?1385522548type =text / javascript>< / script>
application.js
$(document).ready(function(e){
$('FORM')。nestedFields();
});
对于任何想知道的人 - 我终于意识到 - 我的问题是这个---
<%= f.hidden_field:id%>
当II删除它时 - 所有功能正常并处理信息。
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">
<fieldset class="item">
<label for="task_attitudes_attributes_new_nested_item_name">Name</label>
<input id="task_attitudes_attributes_new_nested_item_name" name="task[attitudes_attributes][new_nested_item][name]" size="30" type="text" />
<a href="#" class="remove">remove</a>
<input id="task_attitudes_attributes_new_nested_item_id" name="task[attitudes_attributes][new_nested_item][id]" type="hidden" />
<input id="task_attitudes_attributes_new_nested_item__destroy" name="task[attitudes_attributes][new_nested_item][_destroy]" type="hidden" value="false" />
</fieldset>
</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();
});
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.
这篇关于awesome_nested_fields gem嵌套窗体不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!