0

I'm on Rails 4 using the latest Cocoon gem. Everything is working perfectly; my only issue is that when I remove nested fields, the add link persists. As in the README, the link for add is in _form while the link for remove is in _[model]_fields. I'm using SimpleForm:

_form.html.erb

<%= simple_form_for(@group) do |f| %>
  <%= f.input :name, :label_html => { class: "col-md-2" }, :input_html => { class: "col-md-10" } %>
  <div id="group_names">
    <%= f.simple_fields_for :group_names do |subgroup| %>
      <%= render 'group_name_fields', :f => subgroup %>
      <div class="links">
        <%= link_to_add_association 'add', f, :group_names %>
      </div>
    <% end %>
  </div>
<% end %>

_group_names_fields.html.erb

<div class="nested-fields">
  <%= f.input :subgroup, :label_html => { class: "col-md-2" }, :input_html => { class: "col-md-10" }, :required => false %>
  <%= link_to_remove_association "remove", f %>
</div>

What did I miss? Note: I am not missing a submit button, submission works as does deletion. It's just that the add link does not go away. For something this simple I am sure I have simply done something wrong, but I cannot seem to spot it. Otherwise, I am thinking about wrapping the add link in an if statement but I'm not sure what that condition could be.

Thanks!

Kei
  • 257
  • 3
  • 13

1 Answers1

0

Answer: <%= link_to_add_association 'add', f, :group_names %> should be outside <%= f.simple_fields_for :group_names do |subgroup| %> loop. Also solved my new current issue, so bonus!

This is the code listed in in the cocoon README in haml:

= simple_form_for @project do |f|
  = f.input :name
  = f.input :description
  %h3 Tasks
  #tasks
    = f.simple_fields_for :tasks do |task|
      = render 'task_fields', :f => task
    .links
      = link_to_add_association 'add task', f, :tasks
  = f.submit

I don't know haml, so I don't know if this is just a haml thing, but .links is not in fact in the f.simple_fields_for loop after all.

Community
  • 1
  • 1
Kei
  • 257
  • 3
  • 13
  • Yep, it is a haml thing. Haml is not that hard, imho it is pretty obvious, it reads more like code and the indentation matters. Same indenation is same "level". The haml documentation is easy to look up, but we also have [ERB examples](https://github.com/nathanvda/cocoon/wiki/ERB-examples) in the wiki for the stubborn. – nathanvda Jul 15 '15 at 12:36
  • now that I look at it, it seems like it should have been obvious... thanks! – Kei Jul 16 '15 at 19:16