3

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

4

3 回答 3

3

您提到了您的 Account 类,但没有提到代表过帐或日志的类。见http://homepages.tcp.co.uk/~m-wigley/gc_wp_ded.html存档)。

使用引用站点的语言,为转移创建的“资源”是一个日记帐(条目),由两个过帐组成,每个过帐到不同的帐户。所以你会想要一个 JournalsController。要添加转移,您将 POST 到 JournalsController 的索引操作。参数将包括日期、金额、debit_account、credit_account、收款人、备忘录等。

在 AccountsController 上使用 REST 将用于创建、更新或删除帐户,而不是帐户包含的发布(交易)。

于 2010-12-08T18:06:38.310 回答
3

执行传输的宁静请求示例。

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

相应的回应。

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"
} }
于 2010-12-10T04:50:10.647 回答
1

RESTful Web 服务一书有一个很好的例子来说明如何解决这个确切的问题,更好的是,这个例子在 Rails 中:)

如果你不能从图书馆检查出来,那该死的,就买那个东西。它并没有那么昂贵,而且它有很多关于如何实现 REST 和 ROA 的有用信息。

于 2010-12-08T18:06:28.200 回答