Reputation: 3818
So I have a web app where a user can create and delete his own events. I am trying to link to a delete action when a user presses on a 'trashcan' icon (a font awesome icon) but am a bit stuck. I need to include the icon inside a link_to "do" block since I want the user to press it to delete his own event.
This is easy to do for a GET route, since I can just link to that view, for example, the code to edit a user's event looks like (I am using the cancan gem for authentication):
<% if can? :update, @event %>
<%= link_to edit_event_path(@event), class: 'user' do %>
<i data-toggle="tooltip" data-placement="left"
title="Edit your event" class="custom-a2">
<i class="fa fa-pencil-square-o fa-2x"> </i>
</i>
<% end %>
The above works. And the previous working link_to code to delete an event is:
<%= link_to 'Destroy', @event, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
So the above also works, but I want it so that when the user presses on a trashcan icon, it would link to the delete action of the events controller. This is confusing because this isn't a GET request, and so I need to pass the specific user and event to a link_to action without actually redirecting the user to a view. Can I please get help? My attempt is as below:
<%= link_to(@event), controller: :events, action: :delete, data: { confirm: 'Are you sure?' }, class: 'user' do %>
<i data-toggle="tooltip" data-placement="left"
title="Delete your event" class="custom-a2">
<i class="fa fa-trash fa-2x"> </i>
</i>
<% end %>
Upvotes: 0
Views: 449
Reputation: 3268
You only need to send the event to the delete action for deleting purpose. this should work:
<%= link_to @event, method: :delete, data: { confirm: 'Are you sure?' }, class: 'user' do %>
<i class="fa fa-trash fa-2x"></i>
<% end %>
Upvotes: 1