I'm having difficulty with an ajax-based 'delete' button.
While the button successfully deletes the object from my database, it doesn't update the index page. This means that the deleted object remains visible on the index until the page is manually refreshed.
In my view:
<%= link_to "Delete Fabric", delete_fabric_path(fabric), method: :delete, data: { confirm: 'Are you sure?' }, :remote => true, :class => 'delete_fabric', class: "btn btn-primary btn-sm"%>
Routes:
delete "/fabrics/:id" => "fabrics#destroy", as: 'delete_fabric'
get "/fabrics/collection" => "fabrics#index", as: "index_fabrics"
Controller:
def destroy
@fabric = Fabric.find(params[:id])
@fabric.destroy
respond_to do |format|
format.html { redirect_to index_fabrics_url }
format.json { head :no_content }
format.js { render layout: false }
</end>
destroy.js.erb:
$('.delete_fabric').bind('ajax:success', function() {
$(this).closest('tr').fadeOut();
});
If anyone can pinpoint where I've gone astray, I'd greatly appreciate it! Many thanks.
This is the tutorial I was following.
Server logs:
Started DELETE "/fabrics/46" for 127.0.0.1 at 2018-08-17 15:57:22 +0800
Processing by FabricsController#destroy as JS
Parameters: {"id"=>"46"}
Fabric Load (1.4ms) SELECT "fabrics".* FROM "fabrics" WHERE "fabrics"."id" = $1 LIMIT $2 [["id", 46], ["LIMIT", 1]]
↳ app/controllers/fabrics_controller.rb:70
(0.4ms) BEGIN
↳ app/controllers/fabrics_controller.rb:71
Fabric Destroy (0.5ms) DELETE FROM "fabrics" WHERE "fabrics"."id" = $1 [["id", 46]]
↳ app/controllers/fabrics_controller.rb:71
(6.3ms) COMMIT
↳ app/controllers/fabrics_controller.rb:71
Rendering fabrics/destroy.js.erb
Rendered fabrics/destroy.js.erb (9.4ms)
Completed 200 OK in 83ms (Views: 30.4ms | ActiveRecord: 8.6ms)
Full view:
<!-- SHOW FABRIC COLLECTION / FABRIC OPTIONS-->
<div class="card-deck">
<% @fabrics.each do |item| %>
<div class="col-sm-6 col-md-4 col-lg-3">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="<%=fabric.image%>">
<div class="card-body">
<h5 class="card-title"><%= fabric.fabric_name%></h5>
<p class="card-text"><%= fabric.fabric_description%></p>
<p class="card-text"><%= fabric.printed%></p>
<!-- Listing fibre types -->
<p class="card-text"><small class="text-muted">Composition:
<% fabric.fibre.each do |fibre| %>
<%=fibre %>
<%end%>
</small></p>
<!-- Listing colours -->
<p class="card-text"><small class="text-muted">Colours:
<% fabric.colour.each do |colour| %>
<%=colour %>
<%end%>
</small></p>
<!-- Listing suitable for -->
<p class="card-text"><small class="text-muted">Suitable For:
<% fabric.suitable_for.each do |item| %>
<%=item %>
<%end%>
</small></p>
<!-- Show page button -->
<%= link_to "Full Details", show_fabrics_path(fabric), class: "btn btn-primary btn-sm" %>
<!-- AJAX DELETE BUTTON IN PROGRESS-->
<%= link_to "Delete Fabric", delete_fabric_path(fabric), method: :delete, data: { confirm: 'Are you sure?' }, :remote => true, :class => 'delete_fabric'%>
<!-- AJAX DELETE BUTTON -->
</div>
</div>
</div>
<% end %>
</div>