Skip to content

Instantly share code, notes, and snippets.

@wethu
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save wethu/f5e2a3127acef470c1e7 to your computer and use it in GitHub Desktop.

Select an option

Save wethu/f5e2a3127acef470c1e7 to your computer and use it in GitHub Desktop.
class Controller < ApplicationController
# This will not increment each child
def index
@form = Form.new(Parent.new)
end
end
class Form
include ActiveModel::Model
def self.model_name
ActiveModel::Name.new(self, nil, 'Parent')
end
delegate :foo, :bar, to: :parent
delegate :baz, :qux, to: :child
def parent
@parent ||= Parent.new
end
# Im hoping this will just vend a child every time it gets used by fields_for
def child
parent.children.build
end
def save params
if valid?
@parent.attributes = params[:form].slice( ... )
@parent.save!
# ...
true
else
false
end
end
end
.form
= form_for @form do |f|
= f.text_field :foo
= f.text_field :bar
/ This child should start with a child_index of [0]
= f.fields_for :children, @form.child do |child|
= child.text_field :baz
= child.text_field :qux
/ ... Then Later I want to add another child, its child_index should be [1]
= f.fields_for :children, @form.child do |child|
= child.text_field :baz
= child.text_field :qux
/ My problem is neither have incremented a child_index names are parent[child][field]
/ Instead of the desired parent[child][0][field]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment