Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e61afed6
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e61afed6
编写于
5月 11, 2009
作者:
J
Jarl Friis
提交者:
Yehuda Katz + Carl Lerche
7月 02, 2009
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
My suggestion to fix ticket 2401 [#2401 state:resolved]
Signed-off-by:
N
Yehuda Katz + Carl Lerche
<
ykatz+clerche@engineyard.com
>
上级
49bdbebc
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
98 addition
and
4 deletion
+98
-4
actionpack/lib/action_view/helpers/form_helper.rb
actionpack/lib/action_view/helpers/form_helper.rb
+6
-4
actionpack/test/template/form_helper_test.rb
actionpack/test/template/form_helper_test.rb
+92
-0
未找到文件。
actionpack/lib/action_view/helpers/form_helper.rb
浏览文件 @
e61afed6
...
@@ -926,6 +926,7 @@ class FormBuilder #:nodoc:
...
@@ -926,6 +926,7 @@ class FormBuilder #:nodoc:
attr_accessor
:object_name
,
:object
,
:options
attr_accessor
:object_name
,
:object
,
:options
def
initialize
(
object_name
,
object
,
template
,
options
,
proc
)
def
initialize
(
object_name
,
object
,
template
,
options
,
proc
)
@nested_child_index
=
{}
@object_name
,
@object
,
@template
,
@options
,
@proc
=
object_name
,
object
,
template
,
options
,
proc
@object_name
,
@object
,
@template
,
@options
,
@proc
=
object_name
,
object
,
template
,
options
,
proc
@default_options
=
@options
?
@options
.
slice
(
:index
)
:
{}
@default_options
=
@options
?
@options
.
slice
(
:index
)
:
{}
if
@object_name
.
to_s
.
match
(
/\[\]$/
)
if
@object_name
.
to_s
.
match
(
/\[\]$/
)
...
@@ -1028,7 +1029,7 @@ def fields_for_with_nested_attributes(association_name, args, block)
...
@@ -1028,7 +1029,7 @@ def fields_for_with_nested_attributes(association_name, args, block)
explicit_child_index
=
args
.
last
[
:child_index
]
if
args
.
last
.
is_a?
(
Hash
)
explicit_child_index
=
args
.
last
[
:child_index
]
if
args
.
last
.
is_a?
(
Hash
)
children
.
map
do
|
child
|
children
.
map
do
|
child
|
fields_for_nested_model
(
"
#{
name
}
[
#{
explicit_child_index
||
nested_child_index
}
]"
,
child
,
args
,
block
)
fields_for_nested_model
(
"
#{
name
}
[
#{
explicit_child_index
||
nested_child_index
(
name
)
}
]"
,
child
,
args
,
block
)
end
.
join
end
.
join
else
else
fields_for_nested_model
(
name
,
explicit_object
||
association
,
args
,
block
)
fields_for_nested_model
(
name
,
explicit_object
||
association
,
args
,
block
)
...
@@ -1046,9 +1047,9 @@ def fields_for_nested_model(name, object, args, block)
...
@@ -1046,9 +1047,9 @@ def fields_for_nested_model(name, object, args, block)
end
end
end
end
def
nested_child_index
def
nested_child_index
(
name
)
@nested_child_index
||=
-
1
@nested_child_index
[
name
]
||=
-
1
@nested_child_index
+=
1
@nested_child_index
[
name
]
+=
1
end
end
end
end
end
end
...
@@ -1056,5 +1057,6 @@ def nested_child_index
...
@@ -1056,5 +1057,6 @@ def nested_child_index
class
<<
Base
class
<<
Base
attr_accessor
:default_form_builder
attr_accessor
:default_form_builder
end
end
Base
.
default_form_builder
=
::
ActionView
::
Helpers
::
FormBuilder
Base
.
default_form_builder
=
::
ActionView
::
Helpers
::
FormBuilder
end
end
actionpack/test/template/form_helper_test.rb
浏览文件 @
e61afed6
...
@@ -21,6 +21,9 @@ def author_attributes=(attributes); end
...
@@ -21,6 +21,9 @@ def author_attributes=(attributes); end
attr_accessor
:comments
attr_accessor
:comments
def
comments_attributes
=
(
attributes
);
end
def
comments_attributes
=
(
attributes
);
end
attr_accessor
:tags
def
tags_attributes
=
(
attributes
);
end
end
end
class
Comment
class
Comment
...
@@ -33,6 +36,50 @@ def to_param; @id; end
...
@@ -33,6 +36,50 @@ def to_param; @id; end
def
name
def
name
@id
.
nil?
?
"new
#{
self
.
class
.
name
.
downcase
}
"
:
"
#{
self
.
class
.
name
.
downcase
}
#
#{
@id
}
"
@id
.
nil?
?
"new
#{
self
.
class
.
name
.
downcase
}
"
:
"
#{
self
.
class
.
name
.
downcase
}
#
#{
@id
}
"
end
end
attr_accessor
:relevances
def
relevances_attributes
=
(
attributes
);
end
end
class
Tag
attr_reader
:id
attr_reader
:post_id
def
initialize
(
id
=
nil
,
post_id
=
nil
);
@id
,
@post_id
=
id
,
post_id
end
def
save
;
@id
=
1
;
@post_id
=
1
end
def
new_record?
;
@id
.
nil?
end
def
to_param
;
@id
;
end
def
value
@id
.
nil?
?
"new
#{
self
.
class
.
name
.
downcase
}
"
:
"
#{
self
.
class
.
name
.
downcase
}
#
#{
@id
}
"
end
attr_accessor
:relevances
def
relevances_attributes
=
(
attributes
);
end
end
class
CommentRelevance
attr_reader
:id
attr_reader
:comment_id
def
initialize
(
id
=
nil
,
comment_id
=
nil
);
@id
,
@comment_id
=
id
,
comment_id
end
def
save
;
@id
=
1
;
@comment_id
=
1
end
def
new_record?
;
@id
.
nil?
end
def
to_param
;
@id
;
end
def
value
@id
.
nil?
?
"new
#{
self
.
class
.
name
.
downcase
}
"
:
"
#{
self
.
class
.
name
.
downcase
}
#
#{
@id
}
"
end
end
class
TagRelevance
attr_reader
:id
attr_reader
:tag_id
def
initialize
(
id
=
nil
,
tag_id
=
nil
);
@id
,
@tag_id
=
id
,
tag_id
end
def
save
;
@id
=
1
;
@tag_id
=
1
end
def
new_record?
;
@id
.
nil?
end
def
to_param
;
@id
;
end
def
value
@id
.
nil?
?
"new
#{
self
.
class
.
name
.
downcase
}
"
:
"
#{
self
.
class
.
name
.
downcase
}
#
#{
@id
}
"
end
end
end
class
Author
<
Comment
class
Author
<
Comment
...
@@ -740,6 +787,51 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut
...
@@ -740,6 +787,51 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut
assert_dom_equal
expected
,
output_buffer
assert_dom_equal
expected
,
output_buffer
end
end
def
test_nested_fields_uses_unique_indices_for_different_collection_associations
@post
.
comments
=
[
Comment
.
new
(
321
)]
@post
.
tags
=
[
Tag
.
new
(
123
),
Tag
.
new
(
456
)]
@post
.
comments
[
0
].
relevances
=
[]
@post
.
tags
[
0
].
relevances
=
[]
@post
.
tags
[
1
].
relevances
=
[]
form_for
(
:post
,
@post
)
do
|
f
|
f
.
fields_for
(
:comments
,
@post
.
comments
[
0
])
do
|
cf
|
concat
cf
.
text_field
(
:name
)
cf
.
fields_for
(
:relevances
,
CommentRelevance
.
new
(
314
))
do
|
crf
|
concat
crf
.
text_field
(
:value
)
end
end
f
.
fields_for
(
:tags
,
@post
.
tags
[
0
])
do
|
tf
|
concat
tf
.
text_field
(
:value
)
tf
.
fields_for
(
:relevances
,
TagRelevance
.
new
(
3141
))
do
|
trf
|
concat
trf
.
text_field
(
:value
)
end
end
f
.
fields_for
(
'tags'
,
@post
.
tags
[
1
])
do
|
tf
|
concat
tf
.
text_field
(
:value
)
tf
.
fields_for
(
:relevances
,
TagRelevance
.
new
(
31415
))
do
|
trf
|
concat
trf
.
text_field
(
:value
)
end
end
end
expected
=
'<form action="http://www.example.com" method="post">'
+
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />'
+
'<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />'
+
'<input id="post_comments_attributes_0_relevances_attributes_0_id" name="post[comments_attributes][0][relevances_attributes][0][id]" type="hidden" value="314" />'
+
'<input id="post_comments_attributes_0_relevances_attributes_0_value" name="post[comments_attributes][0][relevances_attributes][0][value]" size="30" type="text" value="commentrelevance #314" />'
+
'<input id="post_tags_attributes_0_id" name="post[tags_attributes][0][id]" type="hidden" value="123" />'
+
'<input id="post_tags_attributes_0_value" name="post[tags_attributes][0][value]" size="30" type="text" value="tag #123" />'
+
'<input id="post_tags_attributes_0_relevances_attributes_0_id" name="post[tags_attributes][0][relevances_attributes][0][id]" type="hidden" value="3141" />'
+
'<input id="post_tags_attributes_0_relevances_attributes_0_value" name="post[tags_attributes][0][relevances_attributes][0][value]" size="30" type="text" value="tagrelevance #3141" />'
+
'<input id="post_tags_attributes_1_id" name="post[tags_attributes][1][id]" type="hidden" value="456" />'
+
'<input id="post_tags_attributes_1_value" name="post[tags_attributes][1][value]" size="30" type="text" value="tag #456" />'
+
'<input id="post_tags_attributes_1_relevances_attributes_0_id" name="post[tags_attributes][1][relevances_attributes][0][id]" type="hidden" value="31415" />'
+
'<input id="post_tags_attributes_1_relevances_attributes_0_value" name="post[tags_attributes][1][relevances_attributes][0][value]" size="30" type="text" value="tagrelevance #31415" />'
+
'</form>'
assert_dom_equal
expected
,
output_buffer
end
def
test_fields_for
def
test_fields_for
fields_for
(
:post
,
@post
)
do
|
f
|
fields_for
(
:post
,
@post
)
do
|
f
|
concat
f
.
text_field
(
:title
)
concat
f
.
text_field
(
:title
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录