Rails - 使用复选框选择多个对象并选择要执行的操作 [英] Rails - Using checkboxes to select multiple objects and having a choice of actions to perform

查看:142
本文介绍了Rails - 使用复选框选择多个对象并选择要执行的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Id希望能够为对象列表创建复选框。然后向用户提供对所选对象执行的多个操作。也就是说删除,存档等。

Id like to be able to create checkboxes for a list of objects. Then offer the user a number of actions to perform on the objects selected. I.e. delete, archive etc.

我知道ryan的screencasts,但它不解释如何创建指向所选对象的多个动作的链接。

I know of ryan's screencasts but it doesnt explain how to create links to multiple actions for the selected objects. It just showed him create a form_tag with a url to one action and a submit button.

推荐答案

我认为你可以做到这一点两种方式。

I think you can do it in two ways.

首先:您可以在一个表单中添加任意数量的按钮:

First: you can add as many buttons to one form as you want:

<%= f.submit "Action 1" %>
<%= f.submit "Action 2" %>
<%= f.submit "Action 3" %>

所有这些都提交到一个操作,您可以在其中检查:

And all of them are submitted to one action in which you can check:

if params[:commit] == "Action 1"
  do some stuff for action 1
elsif params[:commit] == "Action 2"
  do other stuff
... and so on
end

另一种方法是使用一些js。例如,当用户点击按钮操作2时,它会更改表单中的操作参数并将数据提交到此操作。

Another way is to use some js. On example when user clicks on button "Action 2" it changes "action" parameter in form and submits data to this action.

编辑:
您可以这样做:

In case of translated websites, you can do it like this:

<%= f.submit (I18n.t :action_1) %>

和控制器中:

if params[:commit] == I18n.t :action_1
...
end

并在en.yml中添加:

And in en.yml add:

action_1: Action 1

在pl.yml中添加:

In pl.yml add:

action_1: Akcja 1

等等。

这篇关于Rails - 使用复选框选择多个对象并选择要执行的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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