Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Cocoon Gem: Undefined Method 'new_record?' on link_to_remove_association with Wicked

I have been trying to get some code figured out. I have a form I am trying to use the Wicked and Cocoon gem. Everything works, including the link_to_add_association function. I am rendering a partial for the associated form fields just like Cocoon recommends, and everything seems to be working just find except for the link_to_remove_association function. It returns the following error:

undefined method new_record? for nil:NilClass

Here is my partial that is throwing the error:

<div class="nested-fields">
  <div>
    <%= f.input :address1 %>
  </div>
  <div>
    <%= f.input :address2 %>
  </div>
  <div>
    <%= f.input :city %>
  </div>
  <div>
    <%= f.input :state %>
  </div>
  <div>
    <%= f.input :postal %>
  </div>
  <div>
    <%= link_to_remove_association "remove task", f %>
  </div>
</div>

Here is the view that is calling the partial:

<%= simple_form_for @vendor, url: wizard_path do |f| %>
    <div id="locations">
      <%= f.simple_fields_for :locations do |location| %>
        <%= render 'location_fields', :f => location %>
      <% end %>
      <div class="links">
        <%= link_to_add_association 'add location', f, :locations %>
      </div>
    </div>

    <div class="actions">
      <%= f.submit "Continue" %>
    </div>
<% end %>

Here is the controller action that is calling the view:

class UserStepsController < ApplicationController
  include Wicked::Wizard

  steps :personal, :locations

  def show
    @vendor = current_vendor_user.vendor
    @vendor.locations.build
    render_wizard
  end

Incase it helps, here is the function in cocoon that is throwing the error:

def link_to_remove_association(*args, &block)
  if block_given?
    f            = args.first
    html_options = args.second || {}
    name         = capture(&block)
    link_to_remove_association(name, f, html_options)
  else
    name         = args[0]
    f            = args[1]
    html_options = args[2] || {}

    **is_dynamic = f.object.new_record?**
    html_options[:class] = [html_options[:class], "remove_fields #{is_dynamic ? 'dynamic' : 'existing'}"].compact.join(' ')
    hidden_field_tag("#{f.object_name}[_destroy]") + link_to(name, '#', html_options)
  end
end
like image 588
jah.humes Avatar asked Sep 21 '13 14:09

jah.humes


1 Answers

It turns out that I forgot the accepts_nested_attributes_for method on the Vendor model.

like image 185
jah.humes Avatar answered Oct 24 '22 03:10

jah.humes