4

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.

Community
  • 1
  • 1
brunoghisi
  • 424
  • 3
  • 12

3 Answers3

0

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)");
Kalman
  • 8,001
  • 1
  • 27
  • 45
0

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

AndreDurao
  • 5,600
  • 7
  • 41
  • 61
0

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");
Timitry
  • 2,646
  • 20
  • 26