Ruby on Rails REST 设计问题 - 账户之间转账

发布于 2024-10-06 20:28:30 字数 226 浏览 0 评论 0原文

我有一个 Account 类,想要实现转账屏幕以允许用户在 2 个账户之间转账。

我将如何以 RESTfull 方式实现这个?

我有标准帐户和休息操作,那很好。但我该如何实现转移呢?

通常我只会向帐户控制器和相应的视图添加一个名为“transfer”(调用以渲染屏幕)和“transfer_update”(在提交时调用)的方法,但我认为这非常 RESTfull。

谢谢 乔尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒解孤独 2024-10-13 20:28:30

您提到了您的帐户类别,但没有提到代表帖子或日记的类别。请参阅http://homepages.tcp.co.uk/~m-wigley/ gc_wp_ded.html已存档)。

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

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

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.

分開簡單 2024-10-13 20:28:30

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

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

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"
} }
阪姬 2024-10-13 20:28:30

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

如果你无法从图书馆借出它,那又怎么样,就买这个东西吧。它并没有那么昂贵,并且包含许多有关如何实施 REST 和 ROA 的有用信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文