Rails与nested_form gem的嵌套形式

时间:2014-10-12 17:39:19

标签: ruby-on-rails nested-forms

我正在使用this cool gem ryanb nested_forms,但我无法让它正常工作......

我有两个级别的嵌套表单。

class Etude
    has_many :phases

class Phase
    has_many :affectations

class Affectation

基本上,为了简单起见,我有一个阶段表,每个阶段我都有一个表格单元格,我在其中放置了嵌套的表格。我使用<%= f.object.id %>为每个嵌套表生成一个唯一的id,所以我可以使用data-target。

它适用于第一次嵌套(练习曲中的许多阶段),但是我不能将“影响”放入“体内阶段”中。 他是我的颂歌

_phases.html.erb

<%= nested_form_for @etude, :method => :post, :remote => true do |f| %>
<table class="table table-striped table-bordered table-hover" id="tableau_phases">
    <thead>
        blablabla
    </thead>

    <tbody>
        <%= f.fields_for :phases, :wrapper => false %>
    </tbody>
</table>

<!-- I use an other table tag otherwise new entries would appear after the "add_phase" link -->
<table class="table table-striped table-bordered table-hover">
    <tr><td>
        <%= f.link_to_add t("add_phase"), :phases, :data => { :target => "#tableau_phases" } %>
    </tr></td>
</table>
<% end %>   

_phase_fields.html.erb

<tr>
    My fields
</tr>
<tr>
    <td colspan="5">
    <table id="<%= f.object.id %>" class="table table-striped table-bordered table-hover liste-intervenants">
        <thead>
            stuff
        </thead>
        <tbody><% f.fields_for :affectations %></tbody>
    </table>
    </td>
</tr>
<tr>
    <td colspan="5">
        <%= f.link_to_add "Ajouter un intervenant", :affectations, :data => { :target => "##{f.object.id}"} %>
    </td>
</tr>

_affectation_fields.html.erb

只是一些非常简单的<tr><td></td></tr>内容

问题:

点击链接后添加一个新的效果,它不会把东西放在他的表中,但会创建一个额外的div。他是DOM中的样子

<table id="8354u3r1jh534g2fh">
    <thead>headers</thead>
    <tbody>EMPTY !!!</tbody>
    <div class="fields"> Stuff is added there and not in the tbody !</div>
</table>

这很奇怪,因为我有一个:data => { :target => "##{f.object.id}"}应该处理在tbody中插入日期(我已经检查过了,这个代码实际上输出了好东西)

注意:它对第一级嵌套表单(@etude中的阶段)非常有效

1 个答案:

答案 0 :(得分:0)

我修复了它,以及我在途中发现的其他问题:

FIX原始问题:我忘记了第二级嵌套表单的:wrapper => false

_phase_fields.html.erb

<tr>
    <td colspan="5">
    <table id="<%= f.object.id %>" class="table table-striped table-bordered table-hover liste-intervenants">
        <thead>
            stuff
        </thead>
        <tbody><% f.fields_for :affectations, :wrapper => false %></tbody>
    </table>
    </td>
</tr>

问题2 :现在我仍然发生了奇怪的事情。例如,当我在我的前端有这个时

Etude
  Phase 1
    [nothing]
  Phase 2
    [>> I Click "link_to_add" to add a new nested child "Intervenant 2.1"]

然后结果变为

Etude
  Phase 1
    [Intervenant 2.1]
  Phase 2
    [nothing]

相关的代码片段位于 _phase_fields.html.erb

    <%= f.link_to_add "Ajouter un intervenant", :affectations, :data => { :target => "##{f.object.id}"} %>

现在看来"##{f.object.id}"即使对于不同的“嵌套孩子”也是如此。这实际上是宝石的错误吗?

问题2的FIX

实际上,应根据rails API的建议,使用f.index完成ID生成。

问题3

嵌套嵌套表单不会为动态添加的字段生成良好的ID元素!

_affectation_fields.html.erb

==&GT; <%= f.xxxx %>会产生etude[phases_attributes][new_phases][affectations_attributes][1415653508668][nb_jeh]之类的内容。问题是[new_phase]没有被适当生成的阶段ID替换

问题3的FIX

在每个部分中,在<tr>中包裹<tbody class="fields">或者如果只有一个<tr>,请将.fields类添加到其中

解释: 深度嵌套表单依赖于gem nested_form regexp match&amp; replace Javascript。并且the code使用将查找DOM元素的jquery函数,首先转到最近的.fields祖先。 例如:link_to_remove将转到最近的.fields,隐藏它,并将_destroy设置为true。

相关问题