微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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方法.

原文地址:https://www.jb51.cc/ruby/272046.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐