Skip to content

Instantly share code, notes, and snippets.

@delba
Last active December 17, 2015 11:09
Show Gist options
  • Select an option

  • Save delba/5599997 to your computer and use it in GitHub Desktop.

Select an option

Save delba/5599997 to your computer and use it in GitHub Desktop.
Canceling a deletion
gem 'paranoia'
<ul>
<% @items.each do |item| %>
<li>
<%= item.name %>
<%= link_to 'Delete', item, class: 'delete' %>
</li>
<% end %>
</ul>
class Item < ActiveRecord::Base
acts_as_paranoid
end
restoreLink = (href) ->
a = document.createElement('a')
a.className = 'restore'
a.href = href
a.innerText = 'Restore'
a
deleteLink = (href) ->
a = document.createElement('a')
a.className = 'delete'
a.href = href
a.innerText = 'Delete'
a
$(document).on 'click', 'a.delete', (e) ->
e.preventDefault()
$.ajax
type: 'DELETE'
url: @href
dataType: 'json'
success: =>
@parentElement.replaceChild restoreLink("#{@href}/restore"), this
$(document).on 'click', 'a.restore', (e) ->
e.preventDefault()
$.ajax
type: 'PUT'
url: @href
dataType: 'json'
success: =>
@parentElement.replaceChild deleteLink(@href.replace(/\/restore$/, ''), this
class ItemsController < ApplicationController
before_action :set_item
def destroy
@item.destroy
head :no_content
end
def restore
@item.restore!
head :no_content
end
private
def set_item
@item = Item.find(params[:id])
end
end
Itembox::Application.routes.draw do
resources :items do
put :restore, on: :member
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment