ryanb / nested_form

Rails plugin to conveniently handle multiple models in a single form.
MIT License
1.79k stars 505 forks source link

nested:fieldRemoved:model not being triggered #294

Closed andresilveirah closed 10 years ago

andresilveirah commented 10 years ago

Hello I'm using 0.3.2, ruby 1.9.3p448 and Rails 3.2.14. I'm trying to limit the number of nested fields in a form, in order to implement a has_one association. The problem is that the nested:fieldRemoved: is not being triggered while the nested:fieldAdded:model and nested:fieldRemoved are working perfectly fine. Here are my coffee and view codes:

The form

<%= simple_nested_form_for([:worker, @financial_transaction_type]) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.association :financial_account, prompt: true %>
    <%= f.input :name %>
    <%= f.input :kind %>
    <%= f.input :abbreviation %>
    <%= f.input :complementary_information %>
    <%= f.input :financial_account_locked %>
    <%= f.input :status %>
    <%= f.input :first_transaction_operation %>
    <%= f.input :first_transaction %>
    <%= f.input :saturday_workingday %>
    <%= f.input :sunday_workingday %>
    <%= f.input :comments, input_html: {rows: 3} %>
    <div id="financial_tax" class="row-fluid">
      <legend><%= FinancialTax.model_name.human %></legend>
      <%= f.simple_fields_for :financial_tax do |p| %>
        <%= render "financial_tax_fields", builder: p %>
      <% end %>
    </div><br />
    <%= f.link_to_add "<i class='icon-plus'></i> #{t('helpers.actions.add_model', model: FinancialTax.model_name.human)}".html_safe, :financial_tax, class: 'btn', data: { target: "#financial_tax" } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, class: "btn btn-primary" %>
  </div>
<% end %>

The partial _financial_tax_fields

<div class="row-fluid financial_tax_fields">
<%= builder.association :financial_account %>
<%= builder.association :financial_class %>
<%= builder.association :financial_transaction_type %>
<%= builder.input :value %>
<%= builder.input :kind %>
<%= builder.input :status %>
<%= builder.input :comments, input_html: { rows: 3 } %>
<%= builder.link_to_remove t("helpers.actions.destroy_model",model: FinancialTax.model_name.human), :class => 'btn btn-danger btn-mini btn-center'  %>
</div><!-- .row-fluid -->

Here is the coffee script:

jQuery ->
  fieldsCount = $('form .financial_tax_fields').length
  maxFieldsCount = 1
  $addLink = $('a.add_nested_fields')

  toggleAddLink = ->
    $addLink.toggle fieldsCount < maxFieldsCount

  $(document).on 'nested:fieldAdded:financial_tax', ->
    console.log('added')
    fieldsCount += 1
    toggleAddLink()

  $(document).on 'nested:fieldRemoved:financial_tax', ->
    console.log('removed')
    fieldsCount -= 1
    toggleAddLink()

# count existing nested fields after page was loaded
  toggleAddLink()
lest commented 10 years ago

Looks like this regexp is not working with has_one associations. I've fixed it in 6939ec174c5f88f8c9216ee0b84db022cf63c83a.

Could you please test your app with nested_from gem from the master branch?

gem 'nested_form', github: 'ryanb/nested_form'

Thanks!

andresilveirah commented 10 years ago

Hey @lest I just changed my gemfile to use gem 'nested_form', github: 'ryanb/nested_form' and ran a bundle install on my project folder but the problem still remains. Checking the file gems/nested_form-0.3.2/lib/nested_form/builder_mixin.rb still has the old regex.

Am I doing something wrong? That file was supposed to change after your commit right?

lest commented 10 years ago

You can find gem source location using bundle show nested_form. Could you please push a test application reproducing this issue? It would help to resolve it. Thanks!

andresilveirah commented 10 years ago

When I use the gem 'nested_form', github: 'ryanb/nested_form' there's an error on prompting in the javascript console. Uncaught TypeError: Cannot read property '1' of null (jquery_nested_form:line20) And when I went back to the gem 'nested_form' everything worked fine (except the jquery event for removing the field) As you suggested, I've pushed a small app to show this behavior. https://github.com/andresilveira/nested_form_app

I hope it's useful.

lest commented 10 years ago

Thanks for a test application.

I've fixed this js error in the master branch and tested with your test app (changed this line to "nested:fieldRemoved:comment").

andresilveirah commented 10 years ago

Great! It worked as a charm :D Thanks @lest