在Rails中多次提交按钮/表单 [英] Multiple submit buttons/forms in Rails

查看:88
本文介绍了在Rails中多次提交按钮/表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个rails应用程序,它可以让你转到某个页面,比如/ person /:id。在此页面上显示一组可用资源。我希望每个资源都有一个按钮旁边的按钮,它为该人员预留资源(通过创建一个Allocation模型的新实例)。作为扩展,我希望每个资源都有几个按钮,取消预订并执行其他事情。我还想在一些按钮旁边输入数据,例如来分配资源的一部分。



我的问题是我无法知道如何明智地做到这一点,而无需重复自己,或者有一个非常黑客的控制器。如何在不匹配提交按钮的值部分(按钮上的文本)或使用任何javascript的情况下执行此操作?



另外,如果您有两种形式在页面上,如何设置它,以便在单击任何提交按钮时保存两个表单上的更改? 解决方案

使用jQuery,这就是我所做的:

 < script type =text / javascript> 
$(document).ready(function(){
$ b $('#bulk_print')。click(function(){
var target ='<%= bulk_print_prepaid_vouchers_path (:format => pdf)%>';
$('#prepaidvoucher_bulk_print')。attr('action',target);
$('#prepaidvoucher_bulk_print')。submit() ;
});

$('#bulk_destroy')。click(function(){
var target ='<%= bulk_destroy_prepaid_vouchers_path%>';
$('#prepaidvoucher_bulk_print')。attr('action',target);
$('#prepaidvoucher_bulk_print')。submit();
});

});
< / script>

<%form_tag'#',:method => :post,:id => 'prepaidvoucher_bulk_print'do%>

您的表单详情

< button class =buttontype =submitid =bulk_print>
<%= image_tag(web-app-theme / printer.png,:alt =>打印选定的凭证)%>打印选定的凭证
< / button>

< button class =buttontype =submitid =bulk_destroy>
<%= image_tag(web-app-theme / cross.png,:alt =>删除选定的凭证)%>删除选定优惠券
< / button>

<%end%>

这个想法是基于哪个按钮被点击来即时更改表单动作


I am trying to write a rails application which lets you go to a certain page, say /person/:id. On this page it shows a set of available resources. I want each resource to have a button next to it, which reserves that resource to that person (by creating a new instance of an Allocation model.) As an extension, I'd like several buttons by each resource, that cancel reservations and do other things. I'd also like to input data alongside some of the buttons, e.g. to allocate some % of a resource.

My problem is I can't work out how to sensibly do this without repeating myself, or having a very hacky controller. How can I do this without matching on the value part of the submit buttons (the text on the buttons), or using any javascript?

Additionally, if you have two forms on a page, how do you set it up so changes on both forms are saved when any submit button is clicked?

解决方案

im using jQuery, and this is what i did :

<script type="text/javascript">
    $(document).ready(function() {

        $('#bulk_print').click(function(){
            var target = '<%= bulk_print_prepaid_vouchers_path(:format => :pdf) %>';
            $('#prepaidvoucher_bulk_print').attr('action', target);
            $('#prepaidvoucher_bulk_print').submit();
        });

        $('#bulk_destroy').click(function(){
            var target = '<%= bulk_destroy_prepaid_vouchers_path %>';
            $('#prepaidvoucher_bulk_print').attr('action', target);
            $('#prepaidvoucher_bulk_print').submit();
        });

    });
</script>

<% form_tag '#', :method => :post, :id => 'prepaidvoucher_bulk_print' do %>

your form details

<button class="button" type="submit" id="bulk_print">
   <%= image_tag("web-app-theme/printer.png", :alt => "Print Selected Vouchers") %> Print Selected Vouchers
</button>

<button class="button" type="submit" id="bulk_destroy">
    <%= image_tag("web-app-theme/cross.png", :alt => "Delete Selected Vouchers") %> Delete Selected Vouchers
</button>

<% end %>

The idea is to change the form action on the fly, based on which button is clicked

这篇关于在Rails中多次提交按钮/表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆