Skip to content

Commit 1a62dd0

Browse files
committedMay 18, 2015
Updating records
1 parent d19127f commit 1a62dd0

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed
 

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
@Record = React.createClass
2+
getInitialState: ->
3+
edit: false
4+
5+
handleToggle: (e) ->
6+
e.preventDefault()
7+
@setState edit: !@state.edit
8+
29
handleDelete: (e) ->
310
e.preventDefault()
411
# yeah... jQuery doesn't have a $.delete shortcut method
@@ -9,13 +16,70 @@
916
success: () =>
1017
@props.handleDeleteRecord @props.record
1118

12-
render: ->
19+
handleEdit: (e) ->
20+
e.preventDefault()
21+
data =
22+
title: React.findDOMNode(@refs.title).value
Code has comments. Press enter to view.
23+
date: React.findDOMNode(@refs.date).value
24+
amount: React.findDOMNode(@refs.amount).value
25+
# jQuery doesn't have a $.put shortcut method either
26+
$.ajax
27+
method: 'PUT'
28+
url: "/records/#{ @props.record.id }"
29+
dataType: 'JSON'
30+
data:
31+
record: data
32+
success: (data) =>
33+
@setState edit: false
34+
@props.handleEditRecord @props.record, data
35+
36+
recordRow: ->
1337
React.DOM.tr null,
1438
React.DOM.td null, @props.record.date
1539
React.DOM.td null, @props.record.title
1640
React.DOM.td null, amountFormat(@props.record.amount)
1741
React.DOM.td null,
42+
React.DOM.a
43+
className: 'btn btn-default'
44+
onClick: @handleToggle
45+
'Edit'
1846
React.DOM.a
1947
className: 'btn btn-danger'
2048
onClick: @handleDelete
2149
'Delete'
50+
51+
recordForm: ->
52+
React.DOM.tr null,
53+
React.DOM.td null,
54+
React.DOM.input
55+
className: 'form-control'
56+
type: 'text'
57+
defaultValue: @props.record.date
58+
ref: 'date'
59+
React.DOM.td null,
60+
React.DOM.input
61+
className: 'form-control'
62+
type: 'text'
63+
defaultValue: @props.record.title
64+
ref: 'title'
65+
React.DOM.td null,
66+
React.DOM.input
67+
className: 'form-control'
68+
type: 'number'
69+
defaultValue: @props.record.amount
70+
ref: 'amount'
71+
React.DOM.td null,
72+
React.DOM.a
73+
className: 'btn btn-default'
74+
onClick: @handleEdit
75+
'Update'
76+
React.DOM.a
77+
className: 'btn btn-danger'
78+
onClick: @handleToggle
79+
'Cancel'
80+
81+
render: ->
82+
if @state.edit
83+
@recordForm()
84+
else
85+
@recordRow()

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
records = React.addons.update(@state.records, { $splice: [[index, 1]] })
3030
@replaceState records: records
3131

32+
updateRecord: (record, data) ->
33+
index = @state.records.indexOf record
34+
records = React.addons.update(@state.records, { $splice: [[index, 1, data]] })
35+
@replaceState records: records
36+
3237
render: ->
3338
React.DOM.div
3439
className: 'records'
@@ -52,4 +57,4 @@
5257
React.DOM.th null, 'Actions'
5358
React.DOM.tbody null,
5459
for record in @state.records
55-
React.createElement Record, key: record.id, record: record, handleDeleteRecord: @deleteRecord
60+
React.createElement Record, key: record.id, record: record, handleDeleteRecord: @deleteRecord, handleEditRecord: @updateRecord

‎app/controllers/records_controller.rb

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

16+
def update
17+
@record = Record.find(params[:id])
18+
if @record.update(record_params)
19+
render json: @record
20+
else
21+
render json: @record.errors, status: :unprocessable_entity
22+
end
23+
end
24+
1625
def destroy
1726
@record = Record.find(params[:id])
1827
@record.destroy

0 commit comments

Comments
 (0)
Please sign in to comment.