【问题标题】:ActiveAdmin with has_many problem; undefined method 'new_record?'有 has_many 问题的 ActiveAdmin;未定义的方法'new_record?'
【发布时间】:2011-11-04 14:42:47
【问题描述】:

我正在尝试为与 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

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

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

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

未定义的方法`new_record?'对于 nil:NilClass

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

【问题讨论】:

    标签: ruby-on-rails admin has-many formtastic activeadmin


    【解决方案1】:

    转到您的食谱模型并添加以下行

    accepts_nested_attributes_for :steps
    

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

    【讨论】:

      【解决方案2】:
      class Recipe < ActiveRecord::Base
      
          attr_accessible :step_attributes
      
          has_many :steps
      
          accepts_nested_attributes_for :steps
      
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-17
        相关资源
        最近更新 更多