我一直在尝试解决一些代码问题。我有一个表单,我正在尝试使用Wicked和Cocoon gem。一切都可以工作,包括link_to_add_association
函数。我正在呈现一个部分来显示相关的表单字段,就像Cocoon建议的那样,除了link_to_remove_association
函数之外,似乎一切正常运行,它返回以下错误:
undefined method new_record?
for 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
如果有帮助的话,这是在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