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

jah*_*mes 13 ruby ruby-on-rails wicked-gem cocoon-gem

我一直试图弄清楚一些代码.我有一个表格,我正在尝试使用Wicked和Cocoon宝石.一切正常,包括link_to_add_association功能.我正在为Cocoon推荐的相关表单字段渲染部分,并且除了link_to_remove_association函数之外,一切似乎都在找工作.它返回以下错误:

new_record?nil的未定义方法: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>
Run Code Online (Sandbox Code Playgroud)

以下是调用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 %>
Run Code Online (Sandbox Code Playgroud)

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

class UserStepsController < ApplicationController
  include Wicked::Wizard

  steps :personal, :locations

  def show
    @vendor = current_vendor_user.vendor
    @vendor.locations.build
    render_wizard
  end
Run Code Online (Sandbox Code Playgroud)

如果它有帮助,这里是茧中抛出错误的函数:

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
Run Code Online (Sandbox Code Playgroud)

jah*_*mes 28

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