ruby-on-rails - STRIPE ROR:没有路线匹配费用/新错误
问题描述
我是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 %>
谁能帮忙,我缺少什么?提前致谢。
解决方案
路由文件的代码有一个小错误。
下面提到的代码应该在路由文件的最后。[参考]我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
推荐阅读
- modelica - 为什么 Modelica 中的“der()”运算符只适用于时间变量?
- angular - 无法在 Angular 10 中获取选定的值
- javascript - 为什么 Vue.js 图像组件不起作用?
- c# - Windows 窗体绘图图像质量
- javascript - HTML - 输入要输入的指定文本
- c++ - 如何使用 OpenCV 从我的 Windows Hello/IR 摄像头进行捕捉?
- c# - 将 GUI 元素属性绑定到布尔表达式 - WPF GUI
- android - 使用来自 firebase 的数据创建 ExpandableListView
- lua - Lua 注册表已损坏并将内容存储在 0x3FF00000?
- jooq - 使用 @Type(type = "jsonb") 注释的实体会从 jooq 代码生成器中丢弃