Last active
December 17, 2015 11:09
-
-
Save delba/5599997 to your computer and use it in GitHub Desktop.
Canceling a deletion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gem 'paranoia' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <ul> | |
| <% @items.each do |item| %> | |
| <li> | |
| <%= item.name %> | |
| <%= link_to 'Delete', item, class: 'delete' %> | |
| </li> | |
| <% end %> | |
| </ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Item < ActiveRecord::Base | |
| acts_as_paranoid | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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