Search code examples
ruby-on-railsactiveadminhas-manyformtastic

Has_many in activeadmin


I'd like to generate 3 associated forms for record using has_many. It should be 3 forms, not less not more. Is it possible to do using formtastic's has_many? I made it with javascript but it looks like not the best approach for me.


Solution

  • If you're using >= v0.6.2, you can turn off the "Add New *" by passing the option new_record: false on the f.has_many fieldset.

    f.has_many :children, new_record: false do |child_f|
    

    You can also override your resource building method to create three of your child models:

    controller do
      def build_new_resource
        res = super
        3.times { res.children.build }
      end
    end
    

    This should give you a new model with the correct number of nested forms, and will keep the form from letting the user add extra fields.