Ruby on Rails REST design question - transfer money between accounts

[亡魂溺海] 提交于 2019-12-06 01:32:21
aceofspades

You mention your Account class, but not the class that represents postings or journals. See http://homepages.tcp.co.uk/~m-wigley/gc_wp_ded.html (Archived).

Using the language of the referenced site, the "resource" that's being created for a transfer is a journal (entry), consisting of two postings, each to different accounts. So you would want a JournalsController. To add a transfer you would POST to the index action of the JournalsController. Parameters would include date, amount, debit_account, credit_account, payee, memo, etc.

Using REST on AccountsController would be for creating, updating, or deleting accounts, not postings (transactions) which are contained by accounts.

An example of a restful request to perform a transfer.

POST /transfers HTTP/1.1
Host: restful.bank.com
Content-Type: application/json; charset=utf-8
Accept: application/json

{ "transfer": {
  "source_account_id": "9d2d894c242f391a",
  "destination_account_id": "83ac039d8302abd5"
  "amount": "$200.00"
} }

Corresponding response.

HTTP/1.1 201 Created
Date: #{right-now}
Content-Type: application/json; charset=utf-8
Location: https://restful.bank.com/transfers/938ac39cb5ddccfa

{ "transfer": {
  "id": "938ac39cb5ddccfa",
  "href": "https://restful.bank.com/transfers/938ac39cb5ddccfa",
  "source_account_id": "9d2d894c242f391a",
  "destination_account_id": "83ac039d8302abd5"
  "amount": "$200.00"
} }

The book RESTful Web Services has a good example of how to approach this exact problem, and what's better, the example is in Rails :)

If you can't check it out from a library, what the heck, just buy the thing. It's not that expensive and it has lots of useful information about how to implement REST and ROA.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!