ActiveAdmin with has_many problem; undefined method 'new_record?'

Ruby on-RailsAdminHas ManyFormtasticActiveadmin

Ruby on-Rails Problem Overview


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

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:

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

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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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/formtastic for formtastic documentation

Solution 2 - Ruby on-Rails

class Recipe < ActiveRecord::Base

    attr_accessible :step_attributes

    has_many :steps

    accepts_nested_attributes_for :steps

end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnickpellantView Question on Stackoverflow
Solution 1 - Ruby on-RailsDan GurguiView Answer on Stackoverflow
Solution 2 - Ruby on-Railsuser5439847View Answer on Stackoverflow