Permalink
Browse files

Deleting records

  • Loading branch information...
fervisa committed May 18, 2015
1 parent 8d6f0a4 commit 1a4dc646e53fecebc821c709347aae774e9ef170
@@ -1,6 +1,21 @@
@Record = React.createClass
+ handleDelete: (e) ->
+ e.preventDefault()
+ # yeah... jQuery doesn't have a $.delete shortcut method
+ $.ajax
+ method: 'DELETE'
+ url: "/records/#{ @props.record.id }"
+ dataType: 'JSON'
+ success: () =>
+ @props.handleDeleteRecord @props.record
+
render: ->
React.DOM.tr null,
React.DOM.td null, @props.record.date
React.DOM.td null, @props.record.title
React.DOM.td null, amountFormat(@props.record.amount)
+ React.DOM.td null,
+ React.DOM.a
+ className: 'btn btn-danger'
+ onClick: @handleDelete
+ 'Delete'
@@ -25,6 +25,12 @@
records.push record
@setState records: records
+ deleteRecord: (record) ->
+ records = @state.records.slice()
+ index = records.indexOf record
+ records.splice index, 1
+ @replaceState records: records
+
render: ->
React.DOM.div
className: 'records'
@@ -45,6 +51,7 @@
React.DOM.th null, 'Date'
React.DOM.th null, 'Title'
React.DOM.th null, 'Amount'
+ React.DOM.th null, 'Actions'
React.DOM.tbody null,
for record in @state.records
- React.createElement Record, key: record.id, record: record
+ React.createElement Record, key: record.id, record: record, handleDeleteRecord: @deleteRecord
@@ -13,6 +13,12 @@ def create
end
end
+ def destroy
+ @record = Record.find(params[:id])
+ @record.destroy
+ head :no_content
+ end
+
private
def record_params

0 comments on commit 1a4dc64

Please sign in to comment.