Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

ruby on rails - Adding a delete link for nested attributes

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 %>

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 %>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...