neo
neo

Reputation: 4116

Rails AJAX delete functionality working but not removing itself until refreshed

I'm trying to delete using AJAX with :remote => true on my link_to

The delete is working fine, getting 200 response, and when page is refreshed item is no longer there.

[30/Jul/2014 11:26:57] "DELETE /credit_cards/12198 HTTP/1.1" 200 - 0.2106

I need it to happen dynamically, when button is clicked the column is removed.

My views, this is located in app/views/spree/checkout/payment

.cc-expiration.two.columns.alpha.omega
        %span.mobile-heading.em

        = link_to (image_tag("icons/delete.png")),
            spree.credit_card_url(card),
            :remote => true, :method => :delete,
            :confirm => 'Are you sure?',
            :class => 'delete_card'

Controller:

def destroy
  @card = Spree::CreditCard.find(params["id"])
  @card.destroy

  respond_to do |format|
    format.html { redirect_to credit_cards_url }
    format.json { head :no_content }
    format.js   { render :layout => false }
  end
end

And finally my destroy.js.erb, located in app/views/spree/credit_cards

$('.delete_card').bind('ajax:success', function() {
        $(this).closest('column').fadeOut();
});

Any help would be greatly appreciated, thank you so much!

Upvotes: 2

Views: 605

Answers (1)

agmcleod
agmcleod

Reputation: 13621

Since you're using the destroy.js.erb, the view file is returned by the ajax response, and executed as such. So you don't need to bind anything to an ajax callback, just execute your code:

// destroy.js.erb
$('.delete_card').closest('column').fadeOut();

Upvotes: 1

Related Questions