一个表单中有多个Rails提交按钮(一个remote)

4

你找到了你的问题的答案吗? - Paulo Fidalgo
不完全是,你可以尝试使用另一种形式或额外的JS。 - brunoghisi
你为什么认为你做不到这个?你尝试过什么? - John Naegle
3个回答

0

我遇到了同样的问题,并使用这个答案中的解决方案,在单击相应按钮时通过 JQuery 使表单变为远程或非远程。为此,请将class:'local-button'class:'remote-button'添加到您的提交按钮,id:'my-form' 添加到您的表单,以及以下代码(CoffeeScript):

$(".remote-button").click ->
  $("#my-form").attr("data-remote", true)
$(".local-button").click ->
  $("#my-form").removeAttr("data-remote")
  $("#my-form").removeData("remote");

0

最简单的方法可能是在同一页上有两个不同的表单。请参见下面。

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)");

0
我认为你可以这样做:
在“远程”提交按钮上创建一个绑定:#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");
});

你还需要在远程提交中使用preventDefault


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接