STRIPE ROR:没有路线符合费用/新错误

时间:2018-06-25 07:10:54

标签: ruby-on-rails ruby stripe-payments payment-gateway

我是ROR的新手,

我正在尝试使用以下链接将条带集成到我的ROR项目中: https://stripe.com/docs/checkout/rails

我按照http://localhost:3000/charges/new路线的建议添加了所有内容,这给了我以下错误:

No route matches charges/new error

config / routes.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

charges_controller.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

new.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 个答案:

答案 0 :(得分:0)

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

下面提到的代码应该在路由文件的最后。 [Refernce]并且我在该行下面添加了resources :charges代码,这就是我遇到上述错误的原因。

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

当我将 routes文件的内容更改为:

时,代码开始工作
  Rails.application.routes.draw do

    mount API::Root => '/'


    resources :charges

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


  end