Is there any way to add two submit buttons in one Form and one of the request should be remote? I mean without using extra JS.
My question is related to Rails: Multi-submit buttons in one Form, but what differs is the remote.
Is there any way to add two submit buttons in one Form and one of the request should be remote? I mean without using extra JS.
My question is related to Rails: Multi-submit buttons in one Form, but what differs is the remote.
The easiest way to do this is probably to have 2 different forms on the same page. See below.
HelloController
class HelloController < ApplicationController
def say_hello
puts params
respond_to do |format|
format.html
format.js
end
end
end
routes.rb
TwoForms::Application.routes.draw do
get "hello/get_ready"
post "hello/say_hello"
root to: "hello#get_ready"
end
views/hello/get_ready.html.erb
<h2>Regular hello</h2>
<%= button_to "Regular Hello", hello_say_hello_path, name: "regular" %>
<h2>Ajaxy hello</h2>
<%= button_to "Ajaxy Hello", hello_say_hello_path, name: "ajaxy", remote: true %>
views/hello/say_hello.html.erb
Success (regular)!
<%= params %>
views/hello/say_hello.js.erb
alert("Success (Ajaxy)");
I think you could do this:
Create a bind on the "remote" submit button: #remote_btn_submit
$("#remote_btn_submit").on("click", function(){ //click it's not the best bind perhaps input?
$("#my_form").data("remote", "true");
//or
//$("#my_form").attr("data-remote", "true");
});
You will need a preventDefault on the remote submit also
I had the same problem and used the solution from this answer to make the form remote or non-remote via JQuery when clicking the corresponding buttons. To do this, add e.g. class: 'local-button'
and class: 'remote-button'
, to your submit buttons, id: 'my-form'
to your form, and the following code (CoffeeScript):
$(".remote-button").click ->
$("#my-form").attr("data-remote", true)
$(".local-button").click ->
$("#my-form").removeAttr("data-remote")
$("#my-form").removeData("remote");