2013-07-30 52 views
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 

有沒有人有任何想法?

回答

1
  1. 我不知道爲什麼你在你的路線,當resources :transactions已經生成此路線get "transactions/new"
  2. 沒有[POST] "/transactions/new",只有一個GET /transactions/new(.:format)
  3. 你應該在的form_for使用的實例,而不是一個符號:

    = form_for @transaction do |f| 
    

    它會發送POST請求/transactions

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 
相關問題