Ruby on Rails REST design question - transfer money between accounts

一世执手 提交于 2020-01-02 07:14:25

问题


I have an Account class, want to implement transfer screens to allow a user to transfer money between 2 accounts.

How would I implement this ins RESTfull way?

I have the standard account and rest actions for that, thats fine. But how would I implement transfer?

Normally I would just add a method called "transfer" (called to render the screen) and "transfer_update"(called on submit) to the accounts controller and corresponding views, but I don think this is very RESTfull.

thanks Joel


回答1:


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.




回答2:


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"
} }



回答3:


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.



来源:https://stackoverflow.com/questions/4390561/ruby-on-rails-rest-design-question-transfer-money-between-accounts

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