Ruby-on-rails ActiveAdmin 有 has_many 问题;未定义的方法'new_record?'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7206541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:51:52  来源:igfitidea点击:

ActiveAdmin with has_many problem; undefined method 'new_record?'

ruby-on-railsadminhas-manyformtasticactiveadmin

提问by nickpellant

I'm trying to customise a ActiveAdmin form for a Recipe model that has a has_many relationship with Step.

我正在尝试为与 Step 具有 has_many 关系的 Recipe 模型自定义 ActiveAdmin 表单。

class Recipe < ActiveRecord::Base
  has_many :steps
end

class Step < ActiveRecord::Base
  acts_as_list :scope => :recipe

  belongs_to :recipe
end

I have the following in my ActiveAdmin file with relation to this:

我的 ActiveAdmin 文件中有与此相关的以下内容:

form do |f|
  f.has_many :steps do |ing_f|
    ing_f.inputs
  end
end

The following error is thrown when I try to load the form:

当我尝试加载表单时抛出以下错误:

undefined method `new_record?' for nil:NilClass

未定义的方法“new_record?” 对于零:NilClass

I've isolated it so far to the has_many method but I'm lost past this. Any advice and help would be appreciated!

到目前为止,我已经将它隔离到 has_many 方法,但我已经迷失了这一点。任何建议和帮助将不胜感激!

回答by Dan Gurgui

go to your Recipe model and add the following line

转到您的配方模型并添加以下行

accepts_nested_attributes_for :steps

The line is required by formtastic, not active admin. Check https://github.com/justinfrench/formtasticfor formtastic documentation

该行是 formtastic 所必需的,而不是活动管理员所必需的。检查https://github.com/justinfrench/formtastic以获取 formtastic 文档

回答by user5439847

class Recipe < ActiveRecord::Base

    attr_accessible :step_attributes

    has_many :steps

    accepts_nested_attributes_for :steps

end