Reputation: 7068
Railscasts did a great tutorial on how to do multiple edit from a selection. But I want to build on top of it. What if I want to do more actions (edit and destroy)? This is similar to gmails ability to preform different actions on mail.
I created my form and I have a drop down menu of the action. The form goes to a action in my controller which is supposed to redirect it to the correct action (edit vs destroy). I'm trying to do this with redirect_to and pass my parameters along, but its not working correctly.
def action
if params[:perform_action] == 'edit'
redirect_to :action => 'edit_multiple', :structure_ids => params[:structure_ids]
elsif params[:perform_action] == 'destroy'
redirect_to destroy_multiple_structures_path, :structure_ids => params[:structure_ids]
end
end
But this isn't working properly. My edit_multiple is actually redirected to the show action. Even though I have it set up in my routes and all.
How do I properly pass the parameters on? Or is it a matter of the method its being passed (and if so, how do I change that?) Or what is the proper way to do this (if this isn't it)?
I tired passing params on but that didn't work.
I am using a collection in routes.rb but I can't figure out how to change the redirect_to to be giving a POST. Any ideas?
Upvotes: 0
Views: 475
Reputation: 1111
Gmail-like functionality for delete, archive, etc needs no form, so just create a button or link with the desired functionality. You only need a form for changing content.
Upvotes: 0
Reputation: 7068
Ok, so I can't seem to figure out how to get method to change in redirect_to and I can't figure out how to get the params passed correctly. THEREFORE I "cheated" and used jQuery to change the action of the form itself:
$("#action").change(function(e){
var changeTo = $(e.target);
var value = changeTo.attr("value");
if(value == 'edit'){
$("form").attr("action","/myController/edit_multiple");
$("form").attr("method","POST");
}
if(value == 'destroy'){
$("form").attr("action","/myController/destroy_multiple");
$("form").attr("method","DELETE");
}
});
Its not optimized but it works. And action is the id of the select tag. Hope that helps someone else.
Upvotes: 1