12
私はいくつかのコードを理解しようとしています。私はWickedとコクーンの宝石を使用しようとしているフォームがあります。 link_to_add_association
機能を含むすべてが機能します。私はCocoonが推奨するように関連するフォームフィールドの部分をレンダリングしていますが、すべてがlink_to_remove_association
機能を除いては動作しているようです。これは、次のエラーを返します。Rails Cocoon Gem:未定義のメソッド 'new_record?' on link_to_remove_association on Wicked
未定義のメソッド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
私はそれを推測したことはありませんでした。ありがとう! – Tim