Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ad5c8b0d
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,发现更多精彩内容 >>
提交
ad5c8b0d
编写于
11月 27, 2011
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3776 from nashby/add-namespace-to-form
add namespace options to form_for
上级
5cf56a4e
25592569
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
95 addition
and
2 deletion
+95
-2
actionpack/CHANGELOG.md
actionpack/CHANGELOG.md
+10
-0
actionpack/lib/action_view/helpers/form_helper.rb
actionpack/lib/action_view/helpers/form_helper.rb
+9
-2
actionpack/test/template/form_helper_test.rb
actionpack/test/template/form_helper_test.rb
+76
-0
未找到文件。
actionpack/CHANGELOG.md
浏览文件 @
ad5c8b0d
## Rails 3.2.0 (unreleased) ##
## Rails 3.2.0 (unreleased) ##
*
You can provide a namespace for your form to ensure uniqueness of id attributes on form elements.
The namespace attribute will be prefixed with underscore on the generate HTML id.
*Vasiliy Ermolovich*
Example:
<%= form_for(@offer, :namespace => 'namespace') do |f| %>
<%= f.label :version, 'Version' %>:
<%= f.text_field :version %>
<% end %>
*
Refactor ActionDispatch::ShowExceptions. Controller is responsible for choice to show exceptions.
*Sergey Nartimov*
*
Refactor ActionDispatch::ShowExceptions. Controller is responsible for choice to show exceptions.
*Sergey Nartimov*
It's possible to override +show_detailed_exceptions?+ in controllers to specify which requests should provide debugging information on errors.
It's possible to override +show_detailed_exceptions?+ in controllers to specify which requests should provide debugging information on errors.
...
...
actionpack/lib/action_view/helpers/form_helper.rb
浏览文件 @
ad5c8b0d
...
@@ -158,6 +158,9 @@ def convert_to_model(object)
...
@@ -158,6 +158,9 @@ def convert_to_model(object)
# * <tt>:url</tt> - The URL the form is submitted to. It takes the same
# * <tt>:url</tt> - The URL the form is submitted to. It takes the same
# fields you pass to +url_for+ or +link_to+. In particular you may pass
# fields you pass to +url_for+ or +link_to+. In particular you may pass
# here a named route directly as well. Defaults to the current action.
# here a named route directly as well. Defaults to the current action.
# * <tt>:namespace</tt> - A namespace for your form to ensure uniqueness of
# id attributes on form elements. The namespace attribute will be prefixed
# with underscore on the generate HTML id.
# * <tt>:html</tt> - Optional HTML attributes for the form tag.
# * <tt>:html</tt> - Optional HTML attributes for the form tag.
#
#
# Also note that +form_for+ doesn't create an exclusive scope. It's still
# Also note that +form_for+ doesn't create an exclusive scope. It's still
...
@@ -385,7 +388,7 @@ def apply_form_for_options!(object_or_array, options) #:nodoc:
...
@@ -385,7 +388,7 @@ def apply_form_for_options!(object_or_array, options) #:nodoc:
action
,
method
=
object
.
respond_to?
(
:persisted?
)
&&
object
.
persisted?
?
[
:edit
,
:put
]
:
[
:new
,
:post
]
action
,
method
=
object
.
respond_to?
(
:persisted?
)
&&
object
.
persisted?
?
[
:edit
,
:put
]
:
[
:new
,
:post
]
options
[
:html
].
reverse_merge!
(
options
[
:html
].
reverse_merge!
(
:class
=>
as
?
"
#{
as
}
_
#{
action
}
"
:
dom_class
(
object
,
action
),
:class
=>
as
?
"
#{
as
}
_
#{
action
}
"
:
dom_class
(
object
,
action
),
:id
=>
as
?
"
#{
as
}
_
#{
action
}
"
:
dom_id
(
object
,
action
)
,
:id
=>
as
?
"
#{
as
}
_
#{
action
}
"
:
[
options
[
:namespace
],
dom_id
(
object
,
action
)].
compact
.
join
(
"_"
).
presence
,
:method
=>
method
:method
=>
method
)
)
...
@@ -971,6 +974,7 @@ class InstanceTag
...
@@ -971,6 +974,7 @@ class InstanceTag
def
initialize
(
object_name
,
method_name
,
template_object
,
object
=
nil
)
def
initialize
(
object_name
,
method_name
,
template_object
,
object
=
nil
)
@object_name
,
@method_name
=
object_name
.
to_s
.
dup
,
method_name
.
to_s
.
dup
@object_name
,
@method_name
=
object_name
.
to_s
.
dup
,
method_name
.
to_s
.
dup
@template_object
=
template_object
@template_object
=
template_object
@object_name
.
sub!
(
/\[\]$/
,
""
)
||
@object_name
.
sub!
(
/\[\]\]$/
,
"]"
)
@object_name
.
sub!
(
/\[\]$/
,
""
)
||
@object_name
.
sub!
(
/\[\]\]$/
,
"]"
)
@object
=
retrieve_object
(
object
)
@object
=
retrieve_object
(
object
)
@auto_index
=
retrieve_autoindex
(
Regexp
.
last_match
.
pre_match
)
if
Regexp
.
last_match
@auto_index
=
retrieve_autoindex
(
Regexp
.
last_match
.
pre_match
)
if
Regexp
.
last_match
...
@@ -989,6 +993,7 @@ def to_label_tag(text = nil, options = {}, &block)
...
@@ -989,6 +993,7 @@ def to_label_tag(text = nil, options = {}, &block)
add_default_name_and_id_for_value
(
tag_value
,
name_and_id
)
add_default_name_and_id_for_value
(
tag_value
,
name_and_id
)
options
.
delete
(
"index"
)
options
.
delete
(
"index"
)
options
.
delete
(
"namespace"
)
options
[
"for"
]
||=
name_and_id
[
"id"
]
options
[
"for"
]
||=
name_and_id
[
"id"
]
if
block_given?
if
block_given?
...
@@ -1195,6 +1200,7 @@ def add_default_name_and_id(options)
...
@@ -1195,6 +1200,7 @@ def add_default_name_and_id(options)
options
[
"name"
]
||=
tag_name
+
(
options
[
'multiple'
]
?
'[]'
:
''
)
options
[
"name"
]
||=
tag_name
+
(
options
[
'multiple'
]
?
'[]'
:
''
)
options
[
"id"
]
=
options
.
fetch
(
"id"
){
tag_id
}
options
[
"id"
]
=
options
.
fetch
(
"id"
){
tag_id
}
end
end
options
[
"id"
]
=
[
options
.
delete
(
'namespace'
),
options
[
"id"
]].
compact
.
join
(
"_"
).
presence
end
end
def
tag_name
def
tag_name
...
@@ -1253,7 +1259,7 @@ def initialize(object_name, object, template, options, proc)
...
@@ -1253,7 +1259,7 @@ def initialize(object_name, object, template, options, proc)
@nested_child_index
=
{}
@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
@parent_builder
=
options
[
:parent_builder
]
@parent_builder
=
options
[
:parent_builder
]
@default_options
=
@options
?
@options
.
slice
(
:index
)
:
{}
@default_options
=
@options
?
@options
.
slice
(
:index
,
:namespace
)
:
{}
if
@object_name
.
to_s
.
match
(
/\[\]$/
)
if
@object_name
.
to_s
.
match
(
/\[\]$/
)
if
object
||=
@template
.
instance_variable_get
(
"@
#{
Regexp
.
last_match
.
pre_match
}
"
)
and
object
.
respond_to?
(
:to_param
)
if
object
||=
@template
.
instance_variable_get
(
"@
#{
Regexp
.
last_match
.
pre_match
}
"
)
and
object
.
respond_to?
(
:to_param
)
@auto_index
=
object
.
to_param
@auto_index
=
object
.
to_param
...
@@ -1280,6 +1286,7 @@ def fields_for(record_name, record_object = nil, fields_options = {}, &block)
...
@@ -1280,6 +1286,7 @@ def fields_for(record_name, record_object = nil, fields_options = {}, &block)
fields_options
,
record_object
=
record_object
,
nil
if
record_object
.
is_a?
(
Hash
)
&&
record_object
.
extractable_options?
fields_options
,
record_object
=
record_object
,
nil
if
record_object
.
is_a?
(
Hash
)
&&
record_object
.
extractable_options?
fields_options
[
:builder
]
||=
options
[
:builder
]
fields_options
[
:builder
]
||=
options
[
:builder
]
fields_options
[
:parent_builder
]
=
self
fields_options
[
:parent_builder
]
=
self
fields_options
[
:namespace
]
=
fields_options
[
:parent_builder
].
options
[
:namespace
]
case
record_name
case
record_name
when
String
,
Symbol
when
String
,
Symbol
...
...
actionpack/test/template/form_helper_test.rb
浏览文件 @
ad5c8b0d
...
@@ -935,6 +935,82 @@ def test_form_for_with_nil_index_option_override
...
@@ -935,6 +935,82 @@ def test_form_for_with_nil_index_option_override
assert_dom_equal
expected
,
output_buffer
assert_dom_equal
expected
,
output_buffer
end
end
def
test_form_for_with_namespace
form_for
(
@post
,
:namespace
=>
'namespace'
)
do
|
f
|
concat
f
.
text_field
(
:title
)
concat
f
.
text_area
(
:body
)
concat
f
.
check_box
(
:secret
)
end
expected
=
whole_form
(
'/posts/123'
,
'namespace_edit_post_123'
,
'edit_post'
,
'put'
)
do
"<input name='post[title]' size='30' type='text' id='namespace_post_title' value='Hello World' />"
+
"<textarea name='post[body]' id='namespace_post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>"
+
"<input name='post[secret]' type='hidden' value='0' />"
+
"<input name='post[secret]' checked='checked' type='checkbox' id='namespace_post_secret' value='1' />"
end
assert_dom_equal
expected
,
output_buffer
end
def
test_form_for_with_namespace_with_label
form_for
(
@post
,
:namespace
=>
'namespace'
)
do
|
f
|
concat
f
.
label
(
:title
)
concat
f
.
text_field
(
:title
)
end
expected
=
whole_form
(
'/posts/123'
,
'namespace_edit_post_123'
,
'edit_post'
,
'put'
)
do
"<label for='namespace_post_title'>Title</label>"
+
"<input name='post[title]' size='30' type='text' id='namespace_post_title' value='Hello World' />"
end
assert_dom_equal
expected
,
output_buffer
end
def
test_two_form_for_with_namespace
form_for
(
@post
,
:namespace
=>
'namespace_1'
)
do
|
f
|
concat
f
.
label
(
:title
)
concat
f
.
text_field
(
:title
)
end
expected_1
=
whole_form
(
'/posts/123'
,
'namespace_1_edit_post_123'
,
'edit_post'
,
'put'
)
do
"<label for='namespace_1_post_title'>Title</label>"
+
"<input name='post[title]' size='30' type='text' id='namespace_1_post_title' value='Hello World' />"
end
assert_dom_equal
expected_1
,
output_buffer
form_for
(
@post
,
:namespace
=>
'namespace_2'
)
do
|
f
|
concat
f
.
label
(
:title
)
concat
f
.
text_field
(
:title
)
end
expected_2
=
whole_form
(
'/posts/123'
,
'namespace_2_edit_post_123'
,
'edit_post'
,
'put'
)
do
"<label for='namespace_2_post_title'>Title</label>"
+
"<input name='post[title]' size='30' type='text' id='namespace_2_post_title' value='Hello World' />"
end
assert_dom_equal
expected_2
,
output_buffer
end
def
test_fields_for_with_namespace
@comment
.
body
=
'Hello World'
form_for
(
@post
,
:namespace
=>
'namespace'
)
do
|
f
|
concat
f
.
text_field
(
:title
)
concat
f
.
text_area
(
:body
)
concat
f
.
fields_for
(
@comment
)
{
|
c
|
concat
c
.
text_field
(
:body
)
}
end
expected
=
whole_form
(
'/posts/123'
,
'namespace_edit_post_123'
,
'edit_post'
,
'put'
)
do
"<input name='post[title]' size='30' type='text' id='namespace_post_title' value='Hello World' />"
+
"<textarea name='post[body]' id='namespace_post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>"
+
"<input name='post[comment][body]' size='30' type='text' id='namespace_post_comment_body' value='Hello World' />"
end
assert_dom_equal
expected
,
output_buffer
end
def
test_submit_with_object_as_new_record_and_locale_strings
def
test_submit_with_object_as_new_record_and_locale_strings
old_locale
,
I18n
.
locale
=
I18n
.
locale
,
:submit
old_locale
,
I18n
.
locale
=
I18n
.
locale
,
:submit
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录