导轨 4 &cocoon gem:无法将第 3 级子项添加到动态添加的第 2 级子项 [英] Rails 4 & cocoon gem: Cannot add 3rd level child to dynamically added 2nd level child

查看:45
本文介绍了导轨 4 &cocoon gem:无法将第 3 级子项添加到动态添加的第 2 级子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 cocoon gem 和 bootstrap/simple_form 来处理我的嵌套表单.我的模型布局如下:

  • 联系人 has_many 目标
  • 一个目标 has_many 任务
  • 任务 has_one 提醒(尚未实现,这是我解决此问题后的下一个)

我的表单布局正确,目标字段构建正确,我可以毫无问题地向我的第一个目标添加/删除任务.但是,当我动态添加另一个目标时(无论它是否在新建或编辑操作中),我无法向其添加/删除任务(如果我单击添加任务",它只会被添加到我的第一个目标中).以下是我的表格:

_form.html.erb

<%= simple_form_for(@contact) do |f|%><% 如果@contact.errors.any?%><div id="error_explanation"><h2><%=pluralize(@contact.errors.count, "error") %>禁止保存此联系人:</h2><ul><% @contact.errors.full_messages.each do |message|%><li><%=消息%></li><%结束%>

<%结束%><%= f.input :name %><%= f.input :title %><%= f.input :company %><%= f.input :email %><%= f.input :notes %><h3>目标:</h3><div id="目标"><%= f.simple_fields_for(:goals) do |goal|%><%= 渲染 'goal_fields', :f =>目标%><div class="links"><%= link_to_add_association '添加目标', f, :goals, :render_options =>{:包装器 =>'引导' } %>

<%结束%>

<%= f.submit :submit %><%结束%>

_goal_fields.html.erb

<%= f.input :title %><%= f.input :due_date %><%= f.input :notes %><%=link_to_remove_association删除目标",f%><h4>任务:</h4><div id="任务"><%= f.simple_fields_for(:tasks) 做 |task|%><%= 渲染 'task_fields', :f =>任务%><div class="links"><%= link_to_add_association '添加任务', f, :tasks, :render_options =>{:包装器 =>'引导' } %>

<%结束%>

task_fields.html.erb

<%= f.input :task_type %><%= f.input :date_of_task %><%= f.input :complete, as: :boolean, checked_value: true, unchecked_value: false %><%= link_to_remove_association "删除任务", f %>

我已经检查了我的代码以确保我使用了正确的 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:

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 &amp;cocoon gem:无法将第 3 级子项添加到动态添加的第 2 级子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆