如何在Rails中为同一表单创建多个提交按钮? [英] How do I create multiple submit buttons for the same form in Rails?

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

问题描述

我需要有多个提交按钮.

I need to have multiple submit buttons.

我有一个创建Contact_Call实例的表单.

I have a form which creates an instance of Contact_Call.

一个按钮可以正常创建它.

One button creates it as normal.

另一个按钮创建了它,但需要具有与默认值不同的:attribute值,并且还需要在控制器中使用的其他但相关的模型上设置属性.

The other button creates it but needs to have a different :attribute value from the default, and it also needs to set the attribute on a different, but related model used in the controller.

我该怎么做?我无法更改路线,因此有没有办法发送由[:params]接收的其他变量?

How do I do that? I can't change the route, so is there a way to send a different variable that gets picked up by [:params]?

如果我这样做了,我该如何在控制器中建立一个case语句?

And if I do then, what do I do in the controller, set up a case statement?

推荐答案

您可以创建多个提交按钮,并为每个按钮提供不同的值:

You can create multiple submit buttons and provide a different value to each:

<% form_for(something) do |f| %>
    ..
    <%= f.submit 'A' %>
    <%= f.submit 'B' %>
    ..
<% end %>

这将输出:

<input type="submit" value="A" id=".." name="commit" />
<input type="submit" value="B" id=".." name="commit" />

在控制器内部,提交的按钮的值将由参数commit标识.检查该值以执行所需的处理:

Inside your controller, the submitted button's value will be identified by the parameter commit. Check the value to do the required processing:

def <controller action>
    if params[:commit] == 'A'
        # A was pressed 
    elsif params[:commit] == 'B'
        # B was pressed
    end
end

但是,请记住,这可能会使您的视图与控制器紧密耦合,这可能不是很理想.

However, remember that this tightly couples your view to the controller which may not be very desirable.

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

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