2013-07-30 38 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 
相关问题