1

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

メソッドを処理できるようにルートを設定するにはどうすればよいですか?前もって感謝します

4

2 に答える 2

1

ここでやろうとしているのは、新しいメソッドを追加することではなく、新しい「HTTP 動詞」を追加することです。やらないでください。次のような厄介なメッセージが表示される可能性があります。

    !! Unexpected error while processing request: EXECUTE, accepted HTTP methods are OPTIONS,
 GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, 
UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL,
 MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, and PATCH

コンソールで ' rake routes' を実行し、実行のルートが登録されていることを確認してください。何かのようなもの:

execute_transaction

次に、実行リンクを更新し、'transaction' を適切なパス ファインダーに置き換え、代わりにメソッドを :post に設定します。

link_to "Execute", execute_transaction_path(transaction), method: :post
于 2012-11-07T02:51:41.453 に答える
0

小さな違い:メソッド名をシンボルから文字列に変更します。

resources :transactions do
  member do
    post "execute"
  end
end

Railsルーティングガイドを確認してください

于 2012-11-07T02:06:12.490 に答える