13

私はいくつかのコードを理解しようとしています。Wicked and Cocoon gemを使用しようとしているフォームがあります。関数を含め、すべてが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, 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
4

1 に答える 1

29

accepts_nested_attributes_forVendor モデルのメソッドを忘れていたことが判明しました。

于 2013-09-22T04:48:07.787 に答える