I currently have a simple form_for that deletes an object. Right now it's a static page that refreshes every time the object is deleted. We are getting a customer request that would like the index page to delete the object without refreshing the page. Obviously ajax can fix this problem but I have very limited experience with javascript. If someone could point me in the right direction I would greatly appreciate it. Here is my code for clarity.
FORM
This is the form that pops up the modal when delete is pressed.
<%= link_to 'Delete', delete_snitch_path(snitch), rel: 'modal:open',
data: { tooltip: "Delete" },
class: 'icon icon-delete delete', title: "Delete #{snitch.name}" %>
-
<%= form_for @snitch, html: { class: "form-actions", method: 'delete' } do |form| %>
<span class="button-text"><%= link_to 'NO WAY!', home_base_url_or_default(root_path), rel: "modal:close" %></span>
<input type="submit" class="button button--modal" value="Yes, delete it.">
<% end %>
Controller
def destroy
DestroySnitch.perform(snitch: @snitch)
respond_to do |format|
format.html do
redirect_to snitches_path, notice: "Snitch was successfully deleted."
end
format.json do
render json: @snitch.all
end
end
end
VIEW
<table class="snitch-list">
<%= render partial: 'snitch', collection: @presenter.snitches %>
</table>
CURRENT JS (NOT WORKING OBVI)
$('ajax').bind('ajax:success', function() {
$(this).closest('tr').fadeOut();
});
Entire snitch partial
<tbody>
<tr class="<%= snitch.classes %>">
<td>
<%= link_to "<span class='icon led'></span><span>#{snitch.name}</span>".html_safe, snitch_path(snitch), class: "name"%>
</td>
<td class="interval"><span class="vspace"><%= snitch.interval %></span></td>
<td class="last-checked">
<span class="vspace">
<% if snitch.source.checked_in_healthy_at %>
<span data-tooltip="Checked in healthy at UTC(<%= snitch.source.checked_in_healthy_at.to_i %>) || LOCAL(<%= snitch.source.checked_in_healthy_at.to_i %>)">
Last seen <strong><%= snitch.checked_in_healthy_at(title: nil) %></strong>
</span>
<% else %>
<strong><%= snitch.checked_in_healthy_at %></strong>
<% end %>
</span>
</td>
<td class="snitch-controls" data-icons="<%= snitch.pauseable? ? "5" : "4" %>">
<%= render 'menu', snitch: snitch %>
<nav class="snitch-states" >
<% if snitch.pauseable? %>
<%= link_to 'Pause', pause_snitch_path(snitch), class: 'icon icon-pause pause',
data: { tooltip: "Pause" },
rel: "modal:open" %>
<% end %>
<%= link_to 'Delete', delete_snitch_path(snitch), rel: 'modal:open',
data: { tooltip: "Delete" },
class: 'icon icon-delete delete', title: "Delete #{snitch.name}" %>
</nav>
</td>
</tr>
</tbody>
I dont have any javascript in place and I'm starting this story just now but I'm already getting confused while googling. also, is there a good way to test ajax with Rspec and Capybara?