【发布时间】:2019-05-11 13:28:58
【问题描述】:
我在 PaymentsController#create 错误/找不到没有 ID 的产品中收到 ActiveRecord::RecordNotFound
我正在尝试设置 Stripe 付款,但在提交信用卡后出现错误。我正在为 Rails 简介课程做这个,我的同学有相同的代码,但他们的代码正在运行。
看来错误在这一行:
@product = Product.find(params[:product_id])
这是我的付款控制器:
class PaymentsController < ApplicationController
def new
@product = @product.payments.new
end
def create
token = params[:stripeToken]
@product = Product.find(params[:product_id])
@user = current_user
# Create the charge on Stripe's servers - this will charge the user's card
begin
charge = Stripe::Charge.create(
amount: (@product.price*100), # amount in cents, again
currency: "usd",
source: token,
description: params[:stripeEmail]
)
if charge.paid
Order.create(
product_id: @product.id,
user_id: @user.id,
total: @product.price
)
#flash[:success] = "You have successfully paid for your order!"
UserMailer.order_confirmation_email(@user, @product).deliver_now
end
rescue Stripe::CardError => e
# The card has been declined
body = e.json_body
err = body[:error]
flash[:error] = "Unfortunately, there was an error processing your payment: #{err[:message]}"
end
redirect_to product_path(@product), notice: "Thank you for your purchase."
end
end
这是控制台消息:
Started POST "/payments/create" for 127.0.0.1 at 2018-12-10 10:49:15 -0500
Processing by PaymentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jJhGPS9Su0IuepHt2Eea/hCEhDo3A3fggu6EUjwLDrJphrC8VmNRycUqzyLGiJpcaN3mHOzr224BYsbgbjo38Q==", "stripeToken"=>"tok_1Dfr0PHrTxlTJx3aoOtOh2Z9", "stripeTokenType"=>"card", "stripeEmail"=>"myemail@gmail.com"}
Completed 404 Not Found in 2ms (ActiveRecord: 0.0ms)
routes.rb
Rails.application.routes.draw do
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout' }
resources :users
resources :products, :invoices, :orders, :users # 5.1 added ", :invoices, :orders, :users"
resources :users, except: [:index]
get 'simple_pages/about'
get 'simple_pages/contact'
get 'simple_pages/index'
get 'simple_pages/landing_page'
post 'simple_pages/thank_you'
post 'payments/create'
root 'simple_pages#landing_page'
mount ActionCable.server => '/cable'
resources :products do # 5.8 added
resources :comments
end
resources :products do
resources :payments
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
payments/_form.html.erb
<%= form_for [@product, @payment] do |f| %>
【问题讨论】:
-
请将您的控制台输出添加到您的问题中。具体来说,
params[:product_id]的值是多少? (请编辑您的问题,不要在 cmets 中回复代码。) -
您能告诉我如何将该值输出到 Rails 控制台吗?
-
在您的 rails 控制台(运行您的 rails 服务器的地方)中,您应该会看到一个以
Started POST开头的事务。它应该包括以Parameters:开头的行。只需将整个内容粘贴到您的问题中即可。 -
谢谢。我已经添加了读数
标签: ruby-on-rails ruby ruby-on-rails-5 stripe-payments