ruby-on-rails – Rails Cocoon Gem:未定义的方法’new_record?’在使用Wicked的link_to_remove_association

我一直在试图找出一些代码.我有一个表单,我试图使用 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>

这是调用部分的视图:

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

Incase它有帮助,这里是茧中的功能,抛出错误

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

解决方法

原来我在vendor模型上忘记了accept_nested_attributes_for方法.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...