2

I'm using https://github.com/ryanb/nested_form for my nested form. The nested form works fine, I'm now looking to have a sort of counter as I want to store the nested attributes in running number sequence. It should count+1 when a link_to_add is used and count-1 when link_to_remove

The nested portion:

<%= f.fields_for :instructions do |instruction| %>
  <%= instruction.label :"instruction #{instruction}" %>
  <%= instruction.text_field :instruction %>
  <%= instruction.link_to_remove "Remove this instruction" %>
<% end %>
<p>
<%= f.link_to_add "Add instruction", :instructions, class: "btn btn-small btn-primary"  %>
</p>

I'm thinking of using a variable to do a simple count, but i'm not sure how to trigger the increment. I've tried adding to the link_to_add, link_to_remove field but it stop with error. I'm still new to rails and pardon me if this a elementary question

4

1 回答 1

1

如果你需要一个序列,你可以使用一个序列化 gem ,例如或者有一个 sequence_no 字段。如果您只需要一个柜台并且不关心订单(或已在控制器/模型中订购),尽管我只会在“展示”中这样做......

<% just_a_counter = 0 %> #creates counter and sets to 0
<%= f.fields_for :instructions do |instruction| %>
  <% just_a_counter += 1 %> #increments counter
  S/No : <%= just_a_counter %> #displays
  #your other code
<% end %>
于 2012-11-11T21:39:59.150 回答