-
Notifications
You must be signed in to change notification settings - Fork 9
Nested forms
Para allows you to create nested forms through its nested fields feature.
When you have a has_many
or has_one
relation associated with a accepts_nested_attributes_for
call, Para automatically detects it as a nested form.
You need to use the custom input type :nested_many
in your field.
Let's say you have a car model with nested wheel instances :
class Car < ActiveRecord::Base
has_many :wheels
accepts_nested_attributes_for :wheels
end
You can the use in your Para form :
= para_form_for(resource) do |form|
= form.input :wheels, as: :nested_many
You'll be presented with a nested form for the wheels.
You can also use : accepts_nested_attributes_for :wheels, allow_destroy: :true
, a "remove" button will be added to the nested fields.
For Nested has_one
relations, juste use the :nested_one
input type.
When you have a nested model, using accepts_nested_attributes_for
, Para will automatically
allow you to add / edit and delete thos nested resources withing the parent form
If you want to override the nested form behavior, you can generate the fields with :
rails g para:nested_fields <your_model_name>
You can pass --container
or -c
option to generate the container partial used for nested many relations to customize collapsed panels, reordering handles, etc.
When the relation model is parent of other models (i.e. you have an STI structure) para will present you with a dropdown to choose which subclass you want to instantiate when creating your nested resource.
class Car < ActiveRecord::Base
has_many :wheels, class_name: 'Wheel::Base'
accepts_nested_attributes_for :wheels
end
class Wheel::Base < ActiveRecord::Base
end
class Wheel::Rounded < Wheel::Base
end
class Wheel::Squared < Wheel::Base
end
In the form, the add button will present you with a dropdown, allowing you to choose between "Rounded" and "Squared".
You can use the usual I18n (activerecord.models.<model_name>
) keys to change and translate the subclasses names in the dropdown.
Also, this feature supports "partial inheritance", which allows you to create any of : app/views/admin/wheel/squareds/_fields
or app/views/admin/wheel/bases/_fields
to overrided the nested fields for that model.