Coder Social home page Coder Social logo

nested new forms about ancestry HOT 9 OPEN

stefankroes avatar stefankroes commented on June 14, 2024 5
nested new forms

from ancestry.

Comments (9)

gshen avatar gshen commented on June 14, 2024

We also have the same problem which make saving nested new forms impossible, it would be super nice if this can be addressed in the near future.

from ancestry.

yxhuvud avatar yxhuvud commented on June 14, 2024

That can be worked around by making a custom save method that validates all children first and then saves them from the root down. This is pretty simple to do.

A bigger problem is that there is nothing that simulates the ability of accepts_nested_attributes_for - there are neither a children= or children_attributes-method. This end up being pretty icky pretty fast, especially in the case of STI.

from ancestry.

gamov avatar gamov commented on June 14, 2024

I've migrated to ancestry... to run in this issue :(
Reverted back to acts_as_tree.

from ancestry.

tommyalvarez avatar tommyalvarez commented on June 14, 2024

+1

from ancestry.

kbrock avatar kbrock commented on June 14, 2024

The children reference the parent id
But it would seem something as common as belongs_to would implement something similar.

anyone have ideas how acts_as_tree or other plugins implement this?

from ancestry.

marcosg avatar marcosg commented on June 14, 2024

This is still relevant today for nested forms and speeding up the creation of seed files using parent.children.build.

from ancestry.

mkralik avatar mkralik commented on June 14, 2024

As a workaround I use custom validate method to validate children attributes and after_create hook to create children. And by implementing active model serializer I return parent with its children and it works fine. It is not super nice solution, but I think it could work for some use cases.

Example, I have a field and children are options for the field. I use STI FieldComponent as parent which implements has_ancestry and FieldInput and FieldOption as abstract models.

Field Model

attr_accessor :options

validate :options_attributes, if: -> field { field.option_field? && field.options != nil }
after_create :create_options, if: -> field { field.option_field? && field.options != nil }

def create_options
  options.each do |option|
    FieldOption.create(option.merge({ parent: self }))
  end
end

def options_attributes
  options.each do |option|
    field_option = FieldOption.new(option)
    if(!field_option.valid?)
      errors.add(:options, field_option.errors )
    end
  end
end

Field Controller

def create
   @field_input =  FieldInput.new(field_input_params)

   if @field_input.save
      render json: @field_input, status: :created, root: 'data'
   else
      render json: @field_input.errors, status: :unprocessable_entity
   end
end

def field_input_params
   params.require(:field).permit(:cf_type, :title, :key, :description, :required, :rank, :hidden, :active, options: [ :id, :key, :title, :rank, :active ])
end

Field Serializer

class Api::V1::Admin::FieldInputSerializer < ActiveModel::Serializer
  attributes :id, :cf_type, :title, :key, :description, :required, :active, :custom, :hidden, :rank, :fieldable_type, :options

  def options
    ActiveModelSerializers::SerializableResource.new(object.children, { adapter: :attributes, namespace:  Api::V1::Admin }).as_json
  end
end

Option Serializer

class Api::V1::Admin::FieldOptionSerializer < ActiveModel::Serializer
  attributes :id, :title, :key, :description, :active, :rank, :children

  def children
    ActiveModelSerializers::SerializableResource.new(object.children, { adapter: :attributes, namespace:  Api::V1::Admin }).as_json
  end
end

from ancestry.

kbrock avatar kbrock commented on June 14, 2024

thank you for sharing. would love it if someone introduced a way to modify the core so this work around is not necessary.

from ancestry.

spacerobotTR avatar spacerobotTR commented on June 14, 2024

Any updates to this? I just started using this with Administrate. It works great for exisiting records, but when I tried to create a new one I got this error:

No child ancestry for new record. Save record before performing tree operations.

Any up to date work arounds for this?

from ancestry.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.