1

I used Railscast episode 198 to create a form that allows me to edit multiple objects individually using checkboxes. I'd like to be able to select "Edit" or "Delete" actions after checking off the boxes of the items I would like to change. I've added this to my photos_controller.rb for the edit action:

 def edit_individual
  @photos = Photo.find(params[:photo_ids])
 end

  def update_individual
   @user = current_user
   @photos = Photo.update(params[:photos].keys, params[:photos].values).reject { |p|   p.errors.empty? }
  if @photos.empty?
    flash[:notice] = "Products updated"
    redirect_to photos_url
  else
    render :action => "edit_individual"
  end
 end

And in my view, I am adding this line of code when I loop through each photo to display it:

<%= form_tag edit_individual_photos_path, :method => "get" do %>
   ... #loop through all photos and add a checkbox
   <%= check_box_tag "photo_ids[]", photo.id %>
<%= submit_tag "Edit", :class => "btn btn-large btn-inverse" %> 

This works fine, but I can't figure out how to add another submit tag to the form to delete the selected items instead of just edit them. Does anyone know how I could pass the array of photo_ids as a parameter and destroy them?

kcurtin
  • 521
  • 1
  • 6
  • 18

2 Answers2

2

With Ashitaka's help, I came up with this for the delete action in my controller. Edit/update was the default action so I only had to specific if the button clicked was "Delete".

In photos_controller.rb :

def edit_individual
  ...
  if params[:commit] == 'Delete'
    @photos = Photo.find(params[:photo_ids])
    @photos.each { |photo|
      photo.remove_image!
      Photo.destroy(photo.id)  }
    redirect_to photos_new_path
  end
end

In the view:

<%= form_tag edit_individual_photos_path do %>
  ...#loop through all of the photos and add checkbox 
  <%= check_box_tag "photo_ids[]", photo.id %>
   #two submit buttons for the different actions
  <%= submit_tag "Edit", :class => "btn btn-large btn-inverse" %> 
  <%= submit_tag "Delete", :class => "btn btn-large btn-danger" %> 
<% end %>
kcurtin
  • 521
  • 1
  • 6
  • 18
1

Duplicate question of How do I create multiple submit buttons for the same form in Rails?.

The only difference is that in that question they are using a form_for and f.submits instead of a form_tag and submit_tags but it should be easy to understand. Your buttons' values will be "Edit" and "Delete" instead of "A" and "B".

Community
  • 1
  • 1
Ashitaka
  • 19,028
  • 6
  • 54
  • 69
  • That tells me how to distinguish between which button is clicked - thanks. I still cannot pass the array of photo_ids to the destroy action and delete all of them. I'm not sure if this is because I am specifiying it is a get request or if there is another reason – kcurtin Mar 19 '12 at 14:49
  • Well, if you do it this way, your ids will all be inside params[:photo_ids] and they will be sent to the edit action. In your edit action you'll have to have an `if params[:commit] == 'Delete'` and inside that iterate through all the ids, find them and then destroy the objects found. – Ashitaka Mar 19 '12 at 22:46
  • cool - thank you. I was able to figure it out. Going to add an answer with the code I ended up using – kcurtin Mar 20 '12 at 14:41