为嵌套属性添加删除链接 [英] Adding a delete link for nested attributes

查看:58
本文介绍了为嵌套属性添加删除链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在show.erb中具有嵌套属性,并且创建了一个空白的嵌套属性,并像下面那样显示了一个项目网格,底部带有空白.

I have nested attributes in my show.erb and I create a blank nested attribute and show a grid of items with the blank at the bottom like so.

<%= form_for @question do |q| %>
  <% q.fields_for :answers, @question.answers do |l| %>
    <tr>

      <td><%= l.text_field :text %></td>
      <td><%= l.check_box :correct %></td>
      <td><%= l.text_field :imagename %></td>
      <td><%= l.number_field :x %></td>
      <td><%= l.number_field :y %></td>
    </tr>
  <% end %>

  <tr>
        <td colspan=5 align=right><%= submit_tag '+' %>
  </tr>

<% end %>

我希望link_to'Destroy'工作,但是当我将它添加到网格时得到undefined method 'plural'

I want a link_to 'Destroy' to work but i'm getting undefined method 'plural' when i add this to the grid

<%= link_to 'Destroy', l, :controller => "answer", :confirm => 'Are you sure?', :method => :delete %>

推荐答案

为什么要使用链接?您还可以在嵌套属性中使用销毁功能.

Why do you want to use a link? You can also use the destroy functionality in the nested attributes.

您需要做的就是在accepts_nested_attributes定义中添加:allow_destroy => true并添加

All you need to do is to add :allow_destroy => true in your accepts_nested_attributes definition and add

<%= l.check_box '_destroy' %>

每个记录. 这样,它会在保存记录时选中所有复选框,从而删除所有嵌套记录.

to each record. That way it removes all the nested records with the check-box checked when saving the record.

这篇关于为嵌套属性添加删除链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆