加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails Cocoon Gem:未定义的方法’new_record

发布时间:2020-12-16 19:50:50 所属栏目:百科 来源:网络整理
导读:我一直在试图找出一些代码.我有一个表单,我试图使用 Wicked和Cocoon宝石.一切正常,包括link_to_add_association功能.我正在像Cocoon所推荐的那样渲染一些关联的表单域,并且除了link_to_remove_association函数之外,一切似乎都在工作.它返回以下错误: 未定义
我一直在试图找出一些代码.我有一个表单,我试图使用 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

Incase它有帮助,这里是茧中的功能,抛出错误:

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方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读