2013-09-21 66 views
12

我一直在試圖找出一些代碼。我有一個表格,我嘗試使用Wicked和Cocoon寶石。一切正常,包括link_to_add_association功能。我正在像Cocoon建議的那樣渲染關聯的表單域的一部分,除了link_to_remove_association函數之外,似乎所有東西都可以正常工作。它返回以下錯誤:Rails Cocoon Gem:未定義方法'new_record?'在link_to_remove_association與邪惡

未定義的方法new_record?爲無: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, 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 

回答

27

事實證明,我忘了在賣方模型accepts_nested_attributes_for方法。

+1

我從來沒有猜到過。謝謝! – Tim

相關問題