提交 6c570648 编写于 作者: R Rafael Mendonça França

Merge pull request #10471 from andyw8/button_to_params

Add params option for button_to

Conflicts:
	actionpack/CHANGELOG.md
* Add `params` option to `button_to` form helper, which renders the given hash
as hidden form fields.
*Andy Waite*
* Make assets helpers work in the controllers like it works in the views.
Example:
......
......@@ -214,6 +214,7 @@ def link_to(name = nil, options = nil, html_options = nil, &block)
# * <tt>:form</tt> - This hash will be form attributes
# * <tt>:form_class</tt> - This controls the class of the form within which the submit button will
# be placed
# * <tt>:params</tt> - Hash of parameters to be rendered as hidden fields within the form.
#
# ==== Data attributes
#
......@@ -288,6 +289,7 @@ def button_to(name = nil, options = nil, html_options = nil, &block)
url = options.is_a?(String) ? options : url_for(options)
remote = html_options.delete('remote')
params = html_options.delete('params')
method = html_options.delete('method').to_s
method_tag = BUTTON_TAG_METHOD_VERBS.include?(method) ? method_tag(method) : ''.html_safe
......@@ -311,6 +313,11 @@ def button_to(name = nil, options = nil, html_options = nil, &block)
end
inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag)
if params
params.each do |name, value|
inner_tags.safe_concat tag(:input, type: "hidden", name: name, value: value.to_param)
end
end
content_tag('form', content_tag('div', inner_tags), form_options)
end
......
......@@ -161,6 +161,13 @@ def test_button_to_with_block
)
end
def test_button_to_with_params
assert_dom_equal(
%{<form action="http://www.example.com" class="button_to" method="post"><div><input type="submit" value="Hello" /><input type="hidden" name="foo" value="bar" /><input type="hidden" name="baz" value="quux" /></div></form>},
button_to("Hello", "http://www.example.com", params: {foo: :bar, baz: "quux"})
)
end
def test_link_tag_with_straight_url
assert_dom_equal %{<a href="http://www.example.com">Hello</a>}, link_to("Hello", "http://www.example.com")
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册