0

I'm making a list of promotions where each should be deleteable. In the view, for each promotion, I've made a button_to button that should run the delete action in the controller when it is clicked. However, all but the FIRST button work correctly. In other words, the first button doesn't work.

in the promotions controller

  def delete
    flash[:success] = 'Your promotion has been successfully deleted!'
    redirect_to('/promotions')
  end

in the promotions view

<% @promotions.each do |p| %>
   <tr>
     <td>
       .
       .
       .
       .
     </td>
     <td>  <%= button_to "Delete" , promotions_delete_path(params:      {promo_code: p.promo_code }),
     method: :post, class: "btn btn-danger btn-xs" %></td>

     <% end %>

I really don't understand why this is happening. Every button but the first reloads the page and renders the flash successfully. The same thing was happening when I attempted to use the javascript confirm with it - it would work for all but the first, so I removed the confirm to see if anything changed - but I still have this issue. I also tried deleting some promotions through the controller and testing the new 'first' button - it doesn't matter what the promotion is, the first button in the list doesn't work. Any help would be appreciated!

1 Answers1

0

It turned out that it was related to the fact that the button_to method uses forms, and thus we had forms within forms (I did not realize that form that <%= @promotions.each %> was inside was relevant, so did not post it.) Changing the button_to to link_to, and using method: post fixed my problem. Now all the 'buttons' work as they should!