Permalink
Browse files

accept a block in button_to helper

Make possible to use a block in button_to helper if button text is hard
to fit into the name parameter, e.g.:

    <%= button_to [:make_happy, @user] do %>
      Make happy <strong><%= @user.name %></strong>
    <% end %>
    # => "<form method="post" action="/users/1/make_happy" class="button_to">
    #      <div>
    #        <button type="submit">
    #          Make happy <strong>Name</strong>
    #        </button>
    #      </div>
    #    </form>"
  • Loading branch information...
1 parent 1376f4c commit ab7a80ea22c94a006788eddfa3b92123b4031cb6 @lest lest committed May 30, 2012
View
@@ -1,5 +1,21 @@
## Rails 4.0.0 (unreleased) ##
+* Make possible to use a block in button_to helper if button text is hard
+ to fit into the name parameter, e.g.:
+
+ <%= button_to [:make_happy, @user] do %>
+ Make happy <strong><%= @user.name %></strong>
+ <% end %>
+ # => "<form method="post" action="/users/1/make_happy" class="button_to">
+ # <div>
+ # <button type="submit">
+ # Make happy <strong>Name</strong>
+ # </button>
+ # </div>
+ # </form>"
+
+ *Sergey Nartimov*
+
* change a way of ordering helpers from several directories. Previously,
when loading helpers from multiple paths, all of the helpers files were
gathered into one array an then they were sorted. Helpers from different
@@ -294,6 +294,16 @@ def link_to(*args, &block)
# # <div><input value="New" type="submit" /></div>
# # </form>"
#
+ # <%= button_to [:make_happy, @user] do %>
+ # Make happy <strong><%= @user.name %></strong>
+ # <% end %>
+ # # => "<form method="post" action="/users/1/make_happy" class="button_to">
+ # # <div>
+ # # <button type="submit">
+ # # Make happy <strong><%= @user.name %></strong>
+ # # </button>
+ # # </div>
+ # # </form>"
#
# <%= button_to "New", :action => "new", :form_class => "new-thing" %>
# # => "<form method="post" action="/controller/new" class="new-thing">
@@ -331,7 +341,16 @@ def link_to(*args, &block)
# # </div>
# # </form>"
# #
- def button_to(name, options = {}, html_options = {})
+ def button_to(*args, &block)
+ if block_given?
+ options = args[0] || {}
+ html_options = args[1] || {}
+ else
+ name = args[0]
+ options = args[1] || {}
+ html_options = args[2] || {}
+ end
+
html_options = html_options.stringify_keys
convert_boolean_attributes!(html_options, %w(disabled))
@@ -350,9 +369,15 @@ def button_to(name, options = {}, html_options = {})
request_token_tag = form_method == 'post' ? token_tag : ''
html_options = convert_options_to_data_attributes(options, html_options)
- html_options.merge!("type" => "submit", "value" => name || url)
+ html_options['type'] = 'submit'
+
+ button = if block_given?
+ content_tag('button', html_options, &block)
+ else
+ tag('input', html_options.merge('value' => name || url))
@claudiofullscreen

claudiofullscreen Sep 15, 2014

@lest I know this is an old commit but… what is the rationale about rendering button_to as:

  1. a <button> if you pass a block, and
  2. an <input type="submit"> if you don't pass a block?
@lest

lest Sep 16, 2014

Contributor

@claudiofullscreen button_to was returning <input type="submit"> before this commit and it shouldn't be changed. Block passing was added in order to put custom HTML inside button (e.g. images) but <input type="submit"> doesn't support HTML inside button text. That is why we need to use <button> when block is passed.

+ end
- inner_tags = method_tag.safe_concat tag('input', html_options).safe_concat request_token_tag
+ inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag)
content_tag('form', content_tag('div', inner_tags), form_options)
end
@@ -144,6 +144,13 @@ def test_button_to_with_method_get
)
end
+ def test_button_to_with_block
+ assert_dom_equal(
+ "<form method=\"post\" action=\"http://www.example.com\" class=\"button_to\"><div><button type=\"submit\"><span>Hello</span></button></div></form>",
+ button_to("http://www.example.com") { content_tag(:span, 'Hello') }
+ )
+ 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

0 comments on commit ab7a80e

Please sign in to comment.