Skip to content

Commit 5309c5d

Browse files
committed
Merge pull request #398 from krzychk/formactions
Add support for "formaction" and "formmethod" attributes in remote forms
2 parents c90885d + 8fc4405 commit 5309c5d

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/rails.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,17 @@
113113
dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
114114

115115
if (element.is('form')) {
116-
method = element.attr('method');
117-
url = element.attr('action');
116+
method = element.data('ujs:submit-button-formmethod') || element.attr('method');
117+
url = element.data('ujs:submit-button-formaction') || element.attr('action');
118118
data = $(element[0].elements).serializeArray();
119119
// memoized value from clicked submit button
120120
var button = element.data('ujs:submit-button');
121121
if (button) {
122122
data.push(button);
123123
element.data('ujs:submit-button', null);
124124
}
125+
element.data('ujs:submit-button-formmethod', null)
126+
element.data('ujs:submit-button-formaction', null)
125127
} else if (element.is(rails.inputChangeSelector)) {
126128
method = element.data('method');
127129
url = element.data('url');
@@ -510,8 +512,10 @@
510512
}
511513
form.data('ujs:submit-button', data);
512514

513-
// Save formnovalidate attribute from button
515+
// Save attributes from button
514516
form.data('ujs:formnovalidate-button', button.attr('formnovalidate'));
517+
form.data('ujs:submit-button-formaction', button.attr('formaction'))
518+
form.data('ujs:submit-button-formmethod', button.attr('formmethod'));
515519
});
516520

517521
$document.delegate(rails.formSubmitSelector, 'ajax:send.rails', function(event) {

test/public/test/call-remote.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ asyncTest('form method is not read from "data-method" attribute in case of missi
3232
});
3333
});
3434

35+
asyncTest('form method is read from submit button "formmethod" if submit is triggered by that button', 1, function() {
36+
var submitButton = $('<input type="submit" formmethod="get">')
37+
buildForm({ method: 'post' });
38+
39+
$('#qunit-fixture').find('form').append(submitButton)
40+
.bind('ajax:success', function(e, data, status, xhr) {
41+
App.assertGetRequest(data);
42+
})
43+
.bind('ajax:complete', function() { start() });
44+
45+
submitButton.trigger('click');
46+
});
47+
3548
asyncTest('form default method is GET', 1, function() {
3649
buildForm();
3750

@@ -56,6 +69,19 @@ asyncTest('form url is read from "action" not "href"', 1, function() {
5669
});
5770
});
5871

72+
asyncTest('form url is read from submit button "formaction" if submit is triggered by that button', 1, function() {
73+
var submitButton = $('<input type="submit" formaction="/echo">')
74+
buildForm({ method: 'post', href: '/echo2' });
75+
76+
$('#qunit-fixture').find('form').append(submitButton)
77+
.bind('ajax:success', function(e, data, status, xhr) {
78+
App.assertRequestPath(data, '/echo');
79+
})
80+
.bind('ajax:complete', function() { start() });
81+
82+
submitButton.trigger('click');
83+
});
84+
5985
asyncTest('prefer JS, but accept any format', 1, function() {
6086
buildForm({ method: 'post' });
6187

0 commit comments

Comments
 (0)