Skip to content

Commit 3e57bc7

Browse files
committed
Merge pull request activeadmin#2071 from shekibobo/has-many-allow-destroy
Add :allow_destroy option to has_many forms
2 parents 608a4e1 + 9cc13d3 commit 3e57bc7

33 files changed

+111
-4
lines changed

docs/5-forms.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,32 @@ Then implement app/views/admin/posts/_form.html.erb:
3737
<%= f.inputs :title, :body %>
3838
<%= f.actions :commit %>
3939
<% end %>
40+
41+
## Nested Resources
42+
43+
You can create forms with nested models using the `has_many` method:
44+
45+
ActiveAdmin.register Post do
46+
47+
form do |f|
48+
f.inputs "Details" do
49+
f.input :title
50+
f.input :published_at, :label => "Publish Post At"
51+
end
52+
f.inputs "Content" do
53+
f.input :body
54+
end
55+
f.inputs do
56+
f.has_many :categories, :allow_destroy => true do |cf|
57+
cf.input :title
58+
end
59+
end
60+
f.actions
61+
end
62+
63+
end
64+
65+
The `:allow_destroy` option will add a checkbox to the end of the nested form allowing
66+
removal of the child object upon submission. Be sure to set `:allow_destroy => true`
67+
on the association to use this option.
68+

lib/active_admin/form_builder.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def has_many(association, options = {}, &block)
6060
contents += template.content_tag(:li, :class => 'has_many_delete') do
6161
template.link_to I18n.t('active_admin.has_many_delete'), "#", :onclick => "$(this).closest('.has_many_fields').remove(); return false;", :class => "button"
6262
end
63+
elsif options[:allow_destroy]
64+
has_many_form.input :_destroy, :as => :boolean, :wrapper_html => {:class => "has_many_remove"},
65+
:label => I18n.t('active_admin.has_many_remove')
66+
6367
end
6468

6569
contents

lib/active_admin/locales/bg.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bg:
1919
download: "Свали:"
2020
has_many_new: "Добави нов %{model}"
2121
has_many_delete: "Изтрий"
22+
has_many_remove: "Премахнете"
2223
filter: "Филтър"
2324
clear_filters: "Изчисти филтрите"
2425
search_field: "Търси %{field}"

lib/active_admin/locales/ca.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ca:
2020
download: "Descarregar:"
2121
has_many_new: "Afegir %{model}"
2222
has_many_delete: "Eliminar"
23+
has_many_remove: "Treure"
2324
filter: "Filtrar"
2425
clear_filters: "Treure filtres"
2526
search_field: "Cercar %{field}"

lib/active_admin/locales/cs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ cs:
2121
download: "Stáhnout:"
2222
has_many_new: "Přidat nový"
2323
has_many_delete: "Odstranit"
24+
has_many_remove: "Odstranit"
2425
filter: "Filtrovat"
2526
clear_filters: "Vyčistit filtry"
2627
search_field: "Prohledat %{field}"

lib/active_admin/locales/da.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ da:
1919
download: "Download:"
2020
has_many_new: "Tilføj ny(t) %{model}"
2121
has_many_delete: "Slet"
22+
has_many_remove: "Fjern"
2223
filter: "Filtrer"
2324
clear_filters: "Ryd filtre"
2425
search_field: "Søg %{field}"

lib/active_admin/locales/de-CH.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
download: "Herunterladen:"
2222
has_many_new: "%{model} hinzufügen"
2323
has_many_delete: "Löschen"
24+
has_many_remove: "Entfernen"
2425
filter: "Filtern"
2526
clear_filters: "Filter entfernen"
2627
search_field: "Durchsuche %{field}"

lib/active_admin/locales/de.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ de:
2121
download: "Herunterladen:"
2222
has_many_new: "%{model} hinzufügen"
2323
has_many_delete: "Löschen"
24+
has_many_remove: "Entfernen"
2425
filter: "Filtern"
2526
clear_filters: "Filter entfernen"
2627
search_field: "Durchsuche %{field}"

lib/active_admin/locales/en-GB.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
download: "Download:"
2222
has_many_new: "Add New %{model}"
2323
has_many_delete: "Delete"
24+
has_many_remove: "Remove"
2425
filter: "Filter"
2526
clear_filters: "Clear Filters"
2627
search_field: "Search %{field}"
@@ -80,4 +81,4 @@
8081
sign_in: "Sign in"
8182
forgot_your_password: "Forgot your password?"
8283
sign_in_with_omniauth_provider: "Sign in with %{provider}"
83-
84+

lib/active_admin/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ en:
2121
download: "Download:"
2222
has_many_new: "Add New %{model}"
2323
has_many_delete: "Delete"
24+
has_many_remove: "Remove"
2425
filter: "Filter"
2526
clear_filters: "Clear Filters"
2627
search_field: "Search %{field}"

0 commit comments

Comments
 (0)