【问题标题】:How to have two submit buttons within a form_for in Rails submit to different actions?如何在 Rails 中的 form_for 中有两个提交按钮提交到不同的操作?
【发布时间】:2012-02-16 18:04:04
【问题描述】:

如何在一个 form_for 方法中有两个提交按钮来提交不同的操作?

我第一次看到这个 question 并点击了这个(相当老的)railscast 答案之一的链接 在这个 Railscast 的结尾,Ryan Bates 建议使用 submit_to_remote 方法将表单提交到不同的操作.我在文档中找不到这个 submit_to_remote 方法(我使用的是 Rails 3.1)。还有办法让不同的提交按钮提交给不同的操作吗?

【问题讨论】:

    标签: ruby-on-rails-3 forms


    【解决方案1】:
     var Widgets = window.Widgets || { };
     Widgets.orders = {
        initialize      : function( ) {
            jQuery('#sumbit1').live('click', function(){
              Widgets.orders.submitOne();
            });
            jQuery('#sumbit2').live('click', function(){
              Widgets.orders.submitTwo();
            });
        },
    
        submitOne :function(num) {
            jQuery.ajax({
              type : "POST",
              url: "/widgets/orders" ,
              data: jQuery('#order_form').serialize(),
              success: function(htmlText){
                if (htmlText.status > 399) {
                  alert('OH NO!!!  Something went wrong!!');
                } else {
                  jQuery('#orders').html(htmlText);
                }
              },
              dataType: 'html'
            });
        },
        submitTwo :function(num) {
            jQuery.ajax({
              type : "POST",
              url: "/blah/orders" ,
              data: jQuery('#order_form').serialize(),
              success: function(htmlText){
                if (htmlText.status > 399) {
                  alert('OH NO!!!  Something went wrong!!');
                } else {
                  jQuery('#orders').html(htmlText);
                }
              },
              dataType: 'html'
            });
        }
    
    // Start it up
    jQuery(function() {
      Widgets.orders.initialize();
    });
    
    };
    

    【讨论】:

      猜你喜欢
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 2012-10-11
      • 2016-06-19
      相关资源
      最近更新 更多