条纹:ActionController::UrlGenerationError |没有路线匹配 |缺少必需的键:[:id] [英] Stripe: ActionController::UrlGenerationError | No route matches | missing required keys: [:id]

查看:44
本文介绍了条纹:ActionController::UrlGenerationError |没有路线匹配 |缺少必需的键:[:id]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在学习 RoR,需要我在 Stripe 集成方面遇到问题.我已经按照此处的说明完成了所有操作,但我已将费用"更改为费用"."

I am just learning RoR and need I am having trouble with Stripe integration. I have done everything as it says here except I have changed "charges" to "charge."

我收到的错误是:没有路由匹配 {:action=>"show", :controller=>"charge"} 缺少必需的键:[:id].

它不让我做:<%= form_tag charge_path do %>

这是我的控制器:

class ChargeController < ApplicationController
def new
end

def create

# Amount in cents
@amount = 0

customer = Stripe::Customer.create(
:email => 'jon@jonkhaykin.com',
:card  => params[:stripeToken]
)

charge = Stripe::Charge.create(
:customer    => customer.id,
:amount      => @amount,
:description => 'Inspire App Charge',
:currency    => 'usd'
)

rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charge_path
end
end

我的 routes.rb 文件有:资源:收费

推荐答案

你不应该偏离 Rails 标准,这在大多数情况下会惩罚你.您应该将控制器重命名回 ChargesController 并查看单一资源",了解如何解决您的问题.

You should not divert from Rails standards, that will most of the time punish you for it. You should rename your controller back to ChargesController and take a look at "Singular Resources" on how you can solve your issue.

因此,您需要进行以下更改以解决问题:

So the changes that you need to fix your issues are as follows:

  1. app/controllers/charge_controller.rb 重命名为 app/controllers/charges_controller.rb
  2. class ChargeController重命名为class ChargesController
  3. resources :charge 替换为 resource :charge
  1. Rename app/controllers/charge_controller.rb to app/controllers/charges_controller.rb
  2. Rename class ChargeController to class ChargesController
  3. Replace resources :charge with resource :charge

通过将 resources :charge 替换为 resource :charge,您将创建一个路径为 /charge 的单一费用资源.

By replaceing resources :charge with resource :charge you will be creating a singular charge resource with the path /charge.

使用您当前的设置,即 resources :charge,您将看到以下内容(这不是您想要的):

With your current setup i.e. resources :charge you will see the following (which is not what you want):

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

正如您在上面看到的,charge_path 解析为 charge#show 但如果您查看路径,它还需要一个 :id 参数您没有在 form_tag :charge_path 调用中提供.

As you can see above the charge_path resolves to charge#show but if you look at the path it also requires an :id parameter which you are not supplying in your form_tag :charge_path call.

这篇关于条纹:ActionController::UrlGenerationError |没有路线匹配 |缺少必需的键:[:id]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆