Rails - Using checkboxes to select multiple objects and having a choice of actions to perform

匿名 (未验证) 提交于 2019-12-03 02:28:01

问题:

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.

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.

回答1:

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 

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.

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

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

and in controller:

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

And in en.yml add:

action_1: Action 1 

In pl.yml add:

action_1: Akcja 1 

and so on.



回答2:

You can always change the name of the submit button. Just look for the params[:button_name] instead of params[:commit].

You'll need some sort of method in the controller to handle the ability to update_many objects. Maybe a before filter to dispatch...



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!