我已经检查了我的代码以确保我使用了正确的 div 类和 ID,但我确定我的表单在某处存在问题.我还为 simple_form/bootstrap 添加了必要的包装器选项.预先感谢您抽出时间帮助我.
这听起来像是 html/javascript 问题.我刚刚在 cocoon 问题上发布了一个类似的问题,所以听起来像是使用 id 而不是类来重复事情.
.
尽管在这种情况下我期望的行为是它显然有效的形式,但它只会在第一个中被保存.不完全是你描述的那样.
其次:是否有意为每个孩子设置添加任务"或添加目标"?
我会按如下方式编写这些循环:
<%= f.simple_fields_for(:tasks) do |task|%><%= 渲染 'task_fields', :f =>任务%><%结束%><div class="links"><%= link_to_add_association '添加任务', f, :tasks, :render_options =>{:包装器 =>'引导' } %><%结束%>
[咆哮]我应该再次尝试推广haml吗?我看到人们在使用 erb 时犯了很多错误,它只是变得混乱(你的缩进很混乱),我有时真的不明白为什么更多的人使用 haml 或 slim.我发现它使我的观点更具可读性.[/rant]
I am currently using the cocoon gem to handle my nested form along with bootstrap/simple_form. My models are laid out like this:
- A Contact
has_many
goals
- A Goal
has_many
tasks
- A Task
has_one
reminder (not yet implemented, this is next once I resolve this issue)
My form is laid out with the Goal fields being built correctly, and I can add/remove tasks to my first Goal without a problem. However, when I dynamically add another Goal (regardless if it's in the new or edit action), I cannot add/remove tasks to it (if I click 'add task', it only gets added to my first Goal). Below are my forms:
_form.html.erb
<%= simple_form_for(@contact) do |f| %>
<% if @contact.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved:</h2>
<ul>
<% @contact.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :name %>
<%= f.input :title %>
<%= f.input :company %>
<%= f.input :email %>
<%= f.input :notes %>
<h3>Goals:</h3>
<div id="goals">
<%= f.simple_fields_for(:goals) do |goal| %>
<%= render 'goal_fields', :f => goal %>
<div class="links">
<%= link_to_add_association 'add goal', f, :goals, :render_options => {:wrapper => 'bootstrap' } %>
</div>
<% end %>
</div>
<%= f.submit :submit %>
<% end %>
_goal_fields.html.erb
<div class="nested-fields">
<%= f.input :title %>
<%= f.input :due_date %>
<%= f.input :notes %>
<%= link_to_remove_association "remove goal", f %>
<h4>Tasks:</h4>
<div id="tasks">
<%= f.simple_fields_for(:tasks) do |task| %>
<%= render 'task_fields', :f => task %>
<div class="links">
<%= link_to_add_association 'add task', f, :tasks, :render_options => {:wrapper => 'bootstrap' } %>
</div>
<% end %>
</div>
</div>
task_fields.html.erb
<div class="nested-fields">
<%= f.input :task_type %>
<%= f.input :date_of_task %>
<%= f.input :complete, as: :boolean, checked_value: true, unchecked_value: false %>
<%= link_to_remove_association "remove task", f %>
</div>
I have gone through my code to make sure that I am using proper div classes and ids, but I'm sure there is a problem with my form somewhere. I also added the necessary wrapper option for simple_form/bootstrap. Thank you in advance for taking the time to helping me out.
解决方案
This sounds like a html/javascript problem. I just had a similar issue posted on cocoon issues, so it sounds like using an id instead of a class for a repeating thing.
Not sure if that causes the problem, but I see you have a <div id='tasks'>
.
Although the behaviour I would expect in that case, would be that in the form it apparently works, but it will only get saved in the first one. Not exactly what you describe.
Secondly: is it intentional to have a "add task" or "add goal" for each child?
I would write those loops as follows:
<%= f.simple_fields_for(:tasks) do |task| %>
<%= render 'task_fields', :f => task %>
<% end %>
<div class="links">
<%= link_to_add_association 'add task', f, :tasks, :render_options => {:wrapper => 'bootstrap' } %>
<% end %>
[rant]
Should I try to promote haml again? I see people making so many errors when using erb, it just gets messy (your indentation is very confusing), I sometimes honestly do not understand why more people use haml or slim. I find it makes my views way more readable.
[/rant]
这篇关于导轨 4 &cocoon gem:无法将第 3 级子项添加到动态添加的第 2 级子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!