Rails Cocoon Gem:Undefined Method'new_record?'在与Wicked的link_to_remove_association上

时间:2013-09-21 14:30:27

标签: ruby-on-rails ruby cocoon-gem wicked-gem

我一直试图找出一些代码。我有一个表单,我正在尝试使用Wicked和Cocoon gem。一切正常,包括link_to_add_association功能。我正在为Cocoon推荐的关联表单字段渲染部分,除了link_to_remove_association函数之外,一切似乎都在寻找。它返回以下错误:

nil的未定义方法new_record?:NilClass

这是我的部分错误:

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

以下是调用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 %>

以下是调用视图的控制器操作:

class UserStepsController < ApplicationController
  include Wicked::Wizard

  steps :personal, :locations

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

包含它有帮助,这是茧中抛出错误的函数:

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

1 个答案:

答案 0 :(得分:28)

事实证明,我忘记了供应商模型上的accepts_nested_attributes_for方法。