Rails - Using checkboxes to select multiple objects and having a choice of actions to perform
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开发者_Go百科.
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
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.
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...
精彩评论