gpt4 book ai didi

ruby-on-rails - Rails 茧 gem : Undefined Method 'new_record?' on link_to_remove_association with Wicked

转载 作者:数据小太阳 更新时间:2023-10-29 06:38:49 27 4
gpt4 key购买 nike

我一直在努力弄清楚一些代码。我有一个表格,我正在尝试使用 Wicked和茧 gem 。一切正常,包括 link_to_add_association 功能。正如 Cocoon 所建议的那样,我正在为关联的表单字段呈现部分内容,并且除了 link_to_remove_association 函数之外,一切似乎都正常工作。它返回以下错误:

nil:NilClass 的未定义方法 new_record?

这是我抛出错误的部分:

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

这是调用部分的 View :

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

这是调用 View 的 Controller 操作:

class UserStepsController < ApplicationController
include Wicked::Wizard

steps :personal, :locations

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

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

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

最佳答案

原来是忘记了Vendor模型上的accepts_nested_attributes_for方法

关于ruby-on-rails - Rails 茧 gem : Undefined Method 'new_record?' on link_to_remove_association with Wicked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18933800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com