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