Railsを使い始めたばかりで、銀行のアプリを作成しようとしています。アカウント間の取引の設定に問題があります。
私は現在、トランザクションとアカウントの足場を組んでいます。私のトランザクションページでは、ソースアカウント、送金金額、および宛先アカウントに関する情報を含む各トランザクションのトランザクションのリストを作成できます。ただし、ページの最後に、ページ上のすべてのトランザクションを処理してページをクリアするリンクまたはボタンが必要です。したがって、指定されたすべての口座残高を変更します。
以下は私がそれについて行ったステップです。
1)トランザクションモデル(transaction.rbモデル)でプロセスメソッドを定義します
class Transaction < ActiveRecord::Base
def proc (transaction)
# Code processes transactions
@account = Account.find(transaction.from_account)
@account.balance = @account.balance - transaction.amount
@account.update_attributes(params[:account]) #update the new balance
end
end
2)次に、transactioncontroller呼び出しexecuteでメソッドを作成します
def execute
@transaction = Transaction.find(params[:id])
proc (@transaction)
@transaction.destroy
respond_to do |format|
format.html { redirect_to transactions_url }
format.json { head :no_content }
end
3)次に、トランザクションページ(以下に表示)に表示するリンクを定義します。
<% @transactions.each do |transaction| %>
<tr>
<td><%= transaction.from_account %></td>
<td><%= transaction.amount %></td>
<td><%= transaction.to_account %></td>
<td><%= link_to 'Execute', transaction, confirm: 'Are you sure?', method: :execute %></td>
<td><%= link_to 'Show', transaction %></td>
<td><%= link_to 'Edit', edit_transaction_path(transaction) %></td>
<td><%= link_to 'Destroy', transaction, confirm: 'Are you sure?', method: :delete %></td>
<td><%= transaction.id%></td>
</tr>
<% end %>
4)しかし、[実行]リンクをクリックすると、ルーティングエラーが発生します:[POST] "/ transaction / 6"
現在、私のルート(routes.rb)は次のように設定されています。
resources :transactions do
member do
post :execute
end
end
resources :accounts
メソッドを処理できるようにルートを設定するにはどうすればよいですか?前もって感謝します