Skip to content

Commit 1a4dc64

Browse files
committedMay 18, 2015
Deleting records
1 parent 8d6f0a4 commit 1a4dc64

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed
 
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
@Record = React.createClass
2+
handleDelete: (e) ->
3+
e.preventDefault()
4+
# yeah... jQuery doesn't have a $.delete shortcut method
5+
$.ajax
6+
method: 'DELETE'
7+
url: "/records/#{ @props.record.id }"
8+
dataType: 'JSON'
9+
success: () =>
10+
@props.handleDeleteRecord @props.record
11+
212
render: ->
313
React.DOM.tr null,
414
React.DOM.td null, @props.record.date
515
React.DOM.td null, @props.record.title
616
React.DOM.td null, amountFormat(@props.record.amount)
17+
React.DOM.td null,
18+
React.DOM.a
19+
className: 'btn btn-danger'
20+
onClick: @handleDelete
21+
'Delete'

‎app/assets/javascripts/components/records.js.coffee

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
records.push record
2626
@setState records: records
2727

28+
deleteRecord: (record) ->
29+
records = @state.records.slice()
30+
index = records.indexOf record
31+
records.splice index, 1
32+
@replaceState records: records
33+
2834
render: ->
2935
React.DOM.div
3036
className: 'records'
@@ -45,6 +51,7 @@
4551
React.DOM.th null, 'Date'
4652
React.DOM.th null, 'Title'
4753
React.DOM.th null, 'Amount'
54+
React.DOM.th null, 'Actions'
4855
React.DOM.tbody null,
4956
for record in @state.records
50-
React.createElement Record, key: record.id, record: record
57+
React.createElement Record, key: record.id, record: record, handleDeleteRecord: @deleteRecord

‎app/controllers/records_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ def create
1313
end
1414
end
1515

16+
def destroy
17+
@record = Record.find(params[:id])
18+
@record.destroy
19+
head :no_content
20+
end
21+
1622
private
1723

1824
def record_params

0 commit comments

Comments
 (0)
Please sign in to comment.