代码之家  ›  专栏  ›  技术社区  ›  Harshal Yeole

条带错误:没有与费用匹配的路由/新错误

  •  0
  • Harshal Yeole  · 技术社区  · 6 年前

    我是RoR的新手,

    我正在尝试整合 条纹 在我的RoR项目中,使用此链接: https://stripe.com/docs/checkout/rails

    我把我去的时候他们建议的所有东西都加进去了。 http://localhost:3000/charges/new 路由,它给出以下错误:

    No route matches charges/new error
    

    配置/路由.rb

    Rails.application.routes.draw do
    
      mount API::Root => '/'
    
      # Getting unmatched routes
      get '*unmatched_route', to: 'application#raise_not_found'
    
      resources :charges
    
    end
    

    以下是生成的路由:

                       charges GET    /charges(.:format)                                           charges#index
                               POST   /charges(.:format)                                           charges#create
                    new_charge GET    /charges/new(.:format)                                       charges#new
                   edit_charge GET    /charges/:id/edit(.:format)                                  charges#edit
                        charge GET    /charges/:id(.:format)                                       charges#show
                               PATCH  /charges/:id(.:format)                                       charges#update
                               PUT    /charges/:id(.:format)                                       charges#update
                               DELETE /charges/:id(.:format)                                       charges#destroy
    

    充电控制器.rb 以下内容:

    class ChargesController < ApplicationController
      def new
      end
    
      def create
        # Amount in cents
        @amount = 500
    
        customer = Stripe::Customer.create(
            :email => params[:stripeEmail],
            :source => params[:stripeToken]
        )
    
        charge = Stripe::Charge.create(
            :customer => customer.id,
            :amount => @amount,
            :description => 'Rails Stripe customer',
            :currency => 'usd'
        )
    
      rescue Stripe::CardError => e
        flash[:error] = e.message
        redirect_to 'new_charge_path'
      end
    end
    

    新.html.erb

    <%= form_tag charges_path do %>
      <article>
        <% if flash[:error].present? %>
          <div id="error_explanation">
            <p><%= flash[:error] %></p>
          </div>
        <% end %>
        <label class="amount">
          <span>Amount: $5.00</span>
        </label>
      </article>
    
      <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
              data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
              data-description="A month's subscription"
              data-amount="500"
              data-locale="auto"></script>
    <% end %>
    

    有人能帮忙吗?我错过了什么?事先谢谢。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Harshal Yeole    6 年前

    路由代码文件中有一个小错误。

    下面提到的代码应该在路由文件的最后一个。 [Refernce] 我补充说 resources :charges 代码低于该行,这就是我得到上述错误的原因。

    # Getting unmatched routes
    get '*unmatched_route', to: 'application#raise_not_found'
    

    当我更改 路由文件 作为:

      Rails.application.routes.draw do
    
        mount API::Root => '/'
    
    
        resources :charges
    
        # Getting unmatched routes
        get '*unmatched_route', to: 'application#raise_not_found'
    
    
      end