Skip to content

Instantly share code, notes, and snippets.

@rorens05
Last active March 21, 2022 01:11
Show Gist options
  • Save rorens05/9b7ed207458f5d97ae12619f33b8a8e6 to your computer and use it in GitHub Desktop.
Save rorens05/9b7ed207458f5d97ae12619f33b8a8e6 to your computer and use it in GitHub Desktop.
# model/product.rb
belongs_to :product, optional: true
# model/variation.rb
has_many :variations, dependent: :destroy
accepts_nested_attributes_for :variations, :allow_destroy => true
# admin/products.rb
permit_params :name, :description,
variations_attributes: [
:id,
:name,
:price,
:_destroy,
:_edit
]
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "Product" do
f.input :name
f.input :status
end
f.has_many :variations,
new_record: 'Add item',
remove_record: 'Remove item',
allow_destroy: true,
class: "mb-5" do |b|
b.input :id, as: :hidden
b.input :name
b.input :price
end
f.actions do
if resource.persisted?
f.action :submit, label: 'Update'
else
f.action :submit, label: 'Create'
end
f.action :cancel, label: 'Cancel'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment