From eed0842cca8c593431abb5fc5afb312996c2963d Mon Sep 17 00:00:00 2001 From: Roberto Thais Date: Wed, 16 Jun 2010 02:49:42 -0400 Subject: [PATCH] Added the possibility of specifying html attributes to the form generated in the button_to url helper --- actionpack/lib/action_view/helpers/url_helper.rb | 58 +++++++++++++++------- actionpack/test/template/url_helper_test.rb | 7 +++ 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 210f148..bc8a16c 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -286,6 +286,7 @@ module ActionView # processed normally, otherwise no action is taken. # * :remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the # submit behaviour. By default this behaviour is an ajax submit. + # *:form_html - Specifies html attributes for the form tag # # ==== Examples # <%= button_to "New", :action => "new" %> @@ -313,34 +314,53 @@ module ActionView # # # # " # # + # # + # <%= button_to('Subscribe', 'http://www.example.com', + # :method => "post", :remote => true, :form_html => {:class => 'large', 'data-type' => 'json'} %> + # # => "
+ # #
+ # # + # #
+ # #
" + # # def button_to(name, options = {}, html_options = {}) - html_options = html_options.stringify_keys - convert_boolean_attributes!(html_options, %w( disabled )) + html_options = html_options.stringify_keys - method_tag = '' - if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s) - method_tag = tag('input', :type => 'hidden', :name => '_method', :value => method.to_s) - end + form_options = html_options.delete('form_html') || {} + form_options = form_options.stringify_keys - form_method = method.to_s == 'get' ? 'get' : 'post' + convert_boolean_attributes!(html_options, %w( disabled )) + convert_boolean_attributes!(form_options, %w( disabled )) - remote = html_options.delete('remote') + method_tag = '' + if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s) + method_tag = tag('input', :type => 'hidden', :name => '_method', :value => method.to_s) + end - request_token_tag = '' - if form_method == 'post' && protect_against_forgery? - request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) - end + form_method = method.to_s == 'get' ? 'get' : 'post' - url = options.is_a?(String) ? options : self.url_for(options) - name ||= url + remote = html_options.delete('remote') - html_options = convert_options_to_data_attributes(options, html_options) + request_token_tag = '' + if form_method == 'post' && protect_against_forgery? + request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) + end - html_options.merge!("type" => "submit", "value" => name) + url = options.is_a?(String) ? options : self.url_for(options) + name ||= url - ("
" + - method_tag + tag("input", html_options) + request_token_tag + "
").html_safe - end + html_options = convert_options_to_data_attributes(options, html_options) + + html_options.merge!("type" => "submit", "value" => name) + + form_class = ["button_to", form_options.delete("class")].compact.join(" ") + form_options.merge!("method" => form_method, "action" => escape_once(url), "class" => form_class) + form_options.merge!("data-remote" => true) if remote + + content_tag :form, form_options do + "
" + method_tag + tag("input", html_options) + request_token_tag + "
" + end.html_safe + end # Creates a link tag of the given +name+ using a URL created by the set of diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 299d6dd..4326a58 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -134,6 +134,13 @@ class UrlHelperTest < ActiveSupport::TestCase button_to("Hello", "http://www.example.com", :method => :get) ) end + + def test_button_to_with_form_options + assert_dom_equal( + "
", + button_to("Hello", "http://www.example.com", :method => :get, :form_html => {:class => "hello", "data-type" => 'json'}) + ) + end def test_link_tag_with_straight_url assert_dom_equal "Hello", link_to("Hello", "http://www.example.com") -- 1.6.5.1