nested_form_fields 0.6.4 → 0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +23 -0
- data/lib/assets/javascripts/nested_form_fields.js.coffee +2 -2
- data/lib/nested_form_fields.rb +22 -10
- data/lib/nested_form_fields/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 494b811bb87fd99fa4b579a0007177634e1beadc
|
4
|
+
data.tar.gz: 2a0cb2875955c11b82eb72ce052cac5ce51444aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d405f5439c1522f6d3b0acb03618d74ec4606125c8f9238b7d1d8fed8bc49d4325ff338bac2e18819a4b3053dc23a2468ee2cf4e9ad8758c640e6c24bdeba078
|
7
|
+
data.tar.gz: 0a8d58d9e5e1f3b98cd3c87a18978dd2da98c29c01da36b65b7ca4f338212c01a8acc25d2ad60cc10334b3a8c244b6a52c7bf8dc0b9f0740f14fe0e565524371
|
data/README.md
CHANGED
@@ -77,6 +77,29 @@ You can pass options like you would to the `content_tag` method by nesting them
|
|
77
77
|
|
78
78
|
= f.nested_fields_for :videos, wrapper_options: { class: 'row' } do |ff|
|
79
79
|
|
80
|
+
If you are using Rails 4 remember to add << NESTED_MODEL >>_attributes and the attributes to the permitted params.
|
81
|
+
Also, if you want to destroy the nested model you should add :_destroy and :id.
|
82
|
+
For example:
|
83
|
+
|
84
|
+
# app/views/users/_form.haml.erb
|
85
|
+
= form_for @user do |f|
|
86
|
+
= f.nested_fields_for :videos do |ff|
|
87
|
+
= ff.remove_nested_fields_link
|
88
|
+
= ff.text_field :video_title
|
89
|
+
..
|
90
|
+
= f.add_nested_fields_link :videos
|
91
|
+
|
92
|
+
# app/controllers/users_controller
|
93
|
+
..
|
94
|
+
def user_params
|
95
|
+
params.require(:user)
|
96
|
+
.permit(:name,:email,videos_attributes:[:video_title,:_destroy,:id])
|
97
|
+
# ^^^ ^^^ ^^^
|
98
|
+
# nested model attrs
|
99
|
+
# they will let you delete the nested model
|
100
|
+
end
|
101
|
+
|
102
|
+
|
80
103
|
There are 4 javascipt events firing before and after addition/removal of the fields in the *nested_form_fields* namespace. Namely:
|
81
104
|
fields_adding, fields_added, fields_removing, fields_removed.
|
82
105
|
|
@@ -10,7 +10,7 @@ nested_form_fields.bind_nested_forms_links = () ->
|
|
10
10
|
$.event.trigger("fields_adding.nested_form_fields",{object_class: object_class, added_index: added_index, association_path: association_path});
|
11
11
|
$template = $("##{association_path}_template")
|
12
12
|
target = $link.attr('data-insert-into')
|
13
|
-
|
13
|
+
|
14
14
|
template_html = $template.html()
|
15
15
|
|
16
16
|
# insert association indexes
|
@@ -34,6 +34,7 @@ nested_form_fields.bind_nested_forms_links = () ->
|
|
34
34
|
$('body').off("click", '.remove_nested_fields_link')
|
35
35
|
$('body').on 'click', '.remove_nested_fields_link', ->
|
36
36
|
$link = $(this)
|
37
|
+
return false unless $.rails.allowAction($link)
|
37
38
|
object_class = $link.data('object-class')
|
38
39
|
delete_association_field_name = $link.data('delete-association-field-name')
|
39
40
|
removed_index = parseInt(delete_association_field_name.match('(\\d+\\]\\[_destroy])')[0][0])
|
@@ -70,4 +71,3 @@ $.fn.closestChild = (selector) ->
|
|
70
71
|
$results
|
71
72
|
else
|
72
73
|
$children.closestChild selector
|
73
|
-
|
data/lib/nested_form_fields.rb
CHANGED
@@ -23,22 +23,34 @@ module ActionView::Helpers
|
|
23
23
|
end
|
24
24
|
|
25
25
|
|
26
|
-
def add_nested_fields_link association, text = nil, html_options = {}
|
26
|
+
def add_nested_fields_link association, text = nil, html_options = {}, &block
|
27
27
|
html_class = html_options.delete(:class) || {}
|
28
28
|
html_data = html_options.delete(:data) || {}
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
|
30
|
+
args = []
|
31
|
+
args << (text || "Add #{association.to_s.singularize.humanize}") unless block_given?
|
32
|
+
args << ''
|
33
|
+
args << { class: "#{html_class} add_nested_fields_link",
|
34
|
+
data: { association_path: association_path(association.to_s),
|
35
|
+
object_class: association.to_s.singularize }.merge(html_data)
|
36
|
+
}.merge(html_options)
|
37
|
+
|
38
|
+
@template.link_to *args, &block
|
33
39
|
end
|
34
40
|
|
35
|
-
def remove_nested_fields_link text = nil, html_options = {}
|
41
|
+
def remove_nested_fields_link text = nil, html_options = {}, &block
|
36
42
|
html_class = html_options.delete(:class) || {}
|
37
43
|
html_data = html_options.delete(:data) || {}
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
44
|
+
|
45
|
+
args = []
|
46
|
+
args << (text || 'x') unless block_given?
|
47
|
+
args << ''
|
48
|
+
args << { class: "#{html_class} remove_nested_fields_link",
|
49
|
+
data: { delete_association_field_name: delete_association_field_name,
|
50
|
+
object_class: @object.class.name.underscore.downcase }.merge(html_data)
|
51
|
+
}.merge(html_options)
|
52
|
+
|
53
|
+
@template.link_to *args, &block
|
42
54
|
end
|
43
55
|
|
44
56
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_form_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.7'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nico Ritsche
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04
|
11
|
+
date: 2015-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|