0

我不太确定为什么会收到此错误:

Routing Error
No route matches [POST] "/transactions/new"

这是我的配置文件:

TwerkApp::Application.routes.draw do
  get "transactions/new"
  resources :transactions

这是我的控制器:

class TransactionsController < ApplicationController
  def new
    @transaction = Transaction.new(current_user.email, 100.0, params[:transaction])
  end

  def create
    @transaction = Transaction.new(current_user.email, 100.0, params[:transaction])
    if @transaction.charge
      flash[:success] = 'Thanks for the moolah!'
      redirect_to root_path
    else
      flash[:error] = @transaction.errors.first
      render :new
    end
  end
end

这是新的交易表格:

= form_for :transaction do |f|
  = label_tag :card_number, "Credit Card Number"
  = text_field_tag :card_number, nil, name: nil, :value => "4111111111111111", class: "cc-number"
  %p
  = label_tag :card_code, "Security Code on Card (CVV)"
  = text_field_tag :card_code, nil, name: nil, :value => "123", class: "cc-csc"
  %p
  = label_tag :card_month, "Card Expiration"
  = select_month nil, {add_month_numbers: true}, {name: nil, class: "cc-em"}
  = select_year Date.new(2020), {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, class: "cc-ey"}
  %br
  = f.submit

和耙路线:

      transactions GET    /transactions(.:format)              transactions#index
                   POST   /transactions(.:format)              transactions#create
   new_transaction GET    /transactions/new(.:format)          transactions#new
  edit_transaction GET    /transactions/:id/edit(.:format)     transactions#edit
       transaction GET    /transactions/:id(.:format)          transactions#show
                   PUT    /transactions/:id(.:format)          transactions#update
                   DELETE /transactions/:id(.:format)          transactions#destroy

有没有人有任何想法?

4

2 回答 2

1
  1. 我不明白为什么你get "transactions/new"的路线中有一个,当你resources :transactions已经生成了这条路线时。
  2. 没有[POST] "/transactions/new",只有一个GET /transactions/new(.:format)
  3. 您应该使用实例而不是 form_for 中的符号:

    = form_for @transaction do |f|
    

    它将发送一个 POST 请求到/transactions.

于 2013-07-30T01:26:16.073 回答
1

控制器中的new方法应更新如下:

class TransactionsController < ApplicationController
  def new
    @transaction = Transaction.new
  end

TransactionsController#create方法也需要更新。该Transaction#new方法被传递了三个参数,但它应该只接受一个散列作为参数。我不确定您的数据库中的字段是什么,但这样的事情应该可以工作:

@transaction = Transaction.new({ email: current_user.email, money: 100.0 }.merge(params[:transaction]))

表格也应该更新:

= form_for :transaction do |f|
  = f.label :card_number, "Credit Card Number"
  = f.text_field :card_number
于 2013-07-30T01:30:04.957 回答